In Depth Guide : Acquia Certified Developer Drupal
  • IN DEPTH GUIDE: ACQUIA CERTIFIED DEVELOPER - DRUPAL 8 EXAM
  • Test Format
  • Domain 1.0: Fundamental Web Development Concepts
    • 1.1 Demonstrate knowledge of HTML and CSS
    • 1.2 Identify Javascript and jQuery programming concepts
    • 1.3 Demonstrate the use of Git for version control
  • Domain 2.0: Site Building
    • 2.1 Demonstrate ability to create and configure Content Types with appropriate fields and field sett
    • 2.2 Demonstrate ability to configure Display Modes for building custom form and view modes for core
    • 2.3 Demonstrate ability to create and use Taxonomy vocabularies and terms for classification and org
    • 2.4 Demonstrate ability to configure Block types, manage Blocks library and configure Block layouts
    • 2.5 Demonstrate ability to build main and alternative navigation systems by using Menus
    • 2.6 Demonstrate ability to create and configure Views for building content list pages, blocks and fe
    • 2.7 Demonstrate ability to use Configuration Management capabilities for exporting site configuratio
    • 2.8 Demonstrate ability to build multilingual websites using core multilingual capabilities
    • 2.9 Demonstrate ability to build RESTful web application using core Web Services capabilities
  • Domain 3.0: Front end development (theming)
    • 3.1 Given a scenario, demonstrate ability to create a custom theme or sub theme.
    • 3.2 Demonstrate knowledge of theming concepts
    • 3.3 Demonstrate ability to use Twig syntax
    • 3.4 Demonstrate ability to build or override Twig templates for defining layout content
    • 3.5 Demonstrate ability to write template pre-process functions for overriding custom output
  • Domain 4.0: Back end development (coding)
    • 4.1 Demonstrate ability to write code using core and Object Oriented PHP
    • 4.2 Demonstrate ability to develop Custom Modules using Drupal API for extending Drupal functionalit
    • 4.3 Demonstrate ability to store and retrieve data using code
    • 4.4 Demonstrate ability to work with other essential APIs
    • 4.5 Demonstrate ability to write code using Drupal Coding Standards
    • 4.6 Demonstrate ability to analyze and resolve site performance issues arising from site configurati
    • 4.7 Demonstrate ability to analyze and resolve security issues arising from site configuration or cu
  • Other Resources
Powered by GitBook
On this page
  • {{ Say Something }}
  • {% Do Something %}
  • {#Comment on Something# }
  • Why Twig?
  • Twig Manipulation
  1. Domain 3.0: Front end development (theming)

3.3 Demonstrate ability to use Twig syntax

Previous3.2 Demonstrate knowledge of theming conceptsNext3.4 Demonstrate ability to build or override Twig templates for defining layout content

Last updated 7 years ago

Review Twig syntax. Review Twig manipulation.

Twig is now the default template engine for D8. In Drupal 7 the default was PHP templates.

3 syntax twig changes to note

{{ Say Something }}
{% Do Something %}
{# Comment on Something #}

{{ Say Something }}

Print Syntax: {{ }} Use the curly braces to print out values of variables. This is similar to the print render() function from Drupal 7.

At the top of classy's page-title.html.twig docs there are variables available by default. The variable {{ title }} will print the entity's title field value. "The page title, for use in the actual content." For example:

{#
/**
* @file
* Theme override for page titles.
*
* Available variables:
* - title_attributes: HTML attributes for the page title element.
* - title_prefix: Additional output populated by modules, intended to be
* displayed in front of the main title tag that appears in the template.
* - title: The page title, for use in the actual content.
* - title_suffix: Additional output populated by modules, intended to be
* displayed after the main title tag that appears in the template.
*/
#}
{{ title_prefix }}
{% if title %}
<h1{{ title_attributes.addClass('page-title') }}>{{ title }}</h1>
{% endif %}
{{ title_suffix }}

{% Do Something %}

The `

syntax is for code syntax. From the above/core/themes/classy/templates/content/page-title.html.twig example.The

` uses the if statement to only print a page title if there is a value.

The `

` can also be used to extend templates.

For example, extend page-title.html.twig would use the original twig template as its base.

{% extends "page-title.html.twig" %}

{#Comment on Something# }

Use the {# #} syntax for comments and documentation.

{#
/**
* @file
* Theme override for page titles.
*
* Available variables:
* - title_attributes: HTML attributes for the page title element.
* - title_prefix: Additional output populated by modules, intended to be
* displayed in front of the main title tag that appears in the template.
* - title: The page title, for use in the actual content.
* - title_suffix: Additional output populated by modules, intended to be
* displayed after the main title tag that appears in the template.
*/
#}

Why Twig?

Twig Manipulation

For example, if you want to join several strings together you can use the safe_join filter. For Example"

{{ items|safe_join(', ') }}

This will print the items value concatenated together with a comma separating each item. Although filters and twig come from the Symfony integration, there are Drupal specific filters which are located at Drupal specific filters are declared in Drupal\Core\Template\TwigExtension::getFilters()

Other Resources

As of Drupal 8, page titles are blocks by default. Therefore, they have their own twig template:

Changing the template engine to use twig helps of functional business logic and the presentation markup. Twig auto-escapes and sanitizes HTML to prevent attacks. Twig Syntax replaces the use of print() and render()

There . Separate filters by from the variable with the | operation. Chaining filters is also possible.

can validate twig errors with blt validate:twig

`

/core/themes/classy/templates/content/page-title.html.twig
separate the concerns
XRSF
are filters you can use to manipulate variables
BLT
Drupal.org Twig in Drupal 8
Drupal.org's Twig best practices - preprocess functions and templates
Filters - Modifying Variables In Twig Templates
Drupal Core's Classy theme templates
Drupalize.me Twig Templating series