Twig templating

Drupal 8's theming layer is complemented by Twig, a component of the Symfony framework. Twig is a template language that uses a syntax similar to Django and Jinja templates. The previous version of Drupal used PHPTemplate that required frontend developers to have a rudimentary understanding of PHP.

In this recipe, we will override the Twig template to provide customizations for the e-mail form element. We will use the basic Twig syntax to add a new class and provide a default placeholder.

Getting ready

This recipe assumes that you have a custom theme created, such as the one you created in the first recipe. When you see mytheme, use the machine name of the theme you created.

Note

At the time of writing this module, the Classy theme does not provide a template suggestion for the e-mail input nor any customizations to the input template that differ from core.

How to do it...

  1. Create a template folder in your theme's base directory to hold your Twig templates.
  2. To begin, you need to copy the input.html.twig file from core/modules/system/templates/input.html.twig to your theme's template folder.
  3. Rename the input.html.twig file to input--email.html.twig in order to use the proper theme hook suggestion, as shown in the following screenshot:
    How to do it...
  4. We will use the addClass twig function to add an input__email class:
    <input{{ attributes.addClass('input__email') }}/>{{ children }}
  5. Above the previous line, we will create a Twig variable using ternary operators to provide a customer placeholder:
    {% set placeholder = attributes.placeholder ? attributes.placeholder : '[email protected]' %}

    This creates a new variable called placeholder using the set operator. The question mark (?) operator checks whether the placeholder property is empty in the attributes object. If it is not empty, it uses the existing value. If the value is empty, it provides a default value.

  6. Go to the Configuration tab and then to Development to rebuild Drupal's cache. We need to do this because Drupal caches the generated Twig output. Any changes made to a Twig template require a cache rebuild.
  7. View an email field or form element and find the modification:
    How to do it...

How it works...

Drupal's theme system is built around hooks and hook suggestions. The element definition of the e-mail input element defines the input__email theme hook. If there is no input__email hook implemented through a Twig template or PHP function, it will step down to just input.

Note

Drupal theme hooks are defined with underscores (_) but use hyphens (-) when used in Twig template files.

A processor, such as Drupal's theme layer, passes variables to Twig. Variables or properties of objects can be printed by wrapping the variable name with curly brackets. All of core's default templates provide information in the file's document block that details the available Twig variables.

Twig has a simplistic syntax with basic logic and functions. The addClass method will take the attributes variable and add the class provided in addition to the existing contents.

When providing a theme hook suggestion or altering an existing template, you will need to rebuild Drupal's cache. The compiled Twig template, as PHP, is cached by Drupal so that Twig does not need to compile each time the template is invoked.

There's more…

Security first

Twig automatically escapes the output by default, making Drupal 8 one of the most secure versions yet. For Drupal 7, as a whole, most security advisors were for cross-site scripting (XSS) vulnerabilities in contributed projects. With Drupal core, using Twig, these security advisories should be severely reduced.

Theme hook suggestions

Drupal utilizes theme hook suggestions as ways to allow output variations based on different conditions. It allows site themes to provide a more specific template for certain instances.

When a theme hook has double underscores (__), Drupal's theme system understands this, and it can break apart the theme hook to find a more generic template. For instance, the e-mail element definition provides input__email as its theme hook. Drupal understands this as follows:

  • Look for a Twig template named input--email.html.twig or a theme hook that defines input__email
  • If you are not satisfied, look for a Twig template named input.html.twig or a theme hook that defines the input

Theme hook suggestions can be provided by the hook_theme_suggestions() hook in a .module or .theme file.

Debugging template file selection and hook suggestions

Debugging can be enabled to inspect the various template files that make up a page and their theme hook suggestions, and check which are active. This can be accomplished by editing the sites/default/services.yml file. If a services.yml file does not exist, copy the default.services.yml to create one.

You need to change debug: false to debug: true under the twig.config section of the file. This will cause the Drupal theming layer to print out the source code comments containing the template information. When debug is on, Drupal will not cache the compiled versions of Twig templates and render them on the fly.

There is another setting that prevents you from having to rebuild Drupal's cache on each template file change, but do not leave debug enabled. The twig.config.auto_reload boolean can be set to true. If this is set to true, the Twig templates will be recompiled if the source code changes.

The Twig logic and operators

The Twig has ternary operators for logic. Using a question mark (?), we can perform a basic is true or not empty operation, whereas a question mark and colon (?:) performs a basic is false or is empty operation.

You may also use the if and else logic to provide different outputs based on variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset