Locating Template Files with Debugging

If you want to know which template is generating the markup for a particular element, you can use Twig's built in debug option. This option will render HTML comments alongside your rendered output that include the theme hooks in use, suggested template filenames, as well as denoting the exact Twig file used to render a particular section of your markup.

Twig Template naming conventions

Drupal loads templates based on certain naming conventions. This allows you to override templates by adding them to your theme and giving them specific names.

After adding a template you must rebuild the cache in order for Drupal to discover your new template.

Twig in Drupal 8

Twig is a template engine for PHP and it is part of the Symfony2 framework.

In Drupal 8 Twig replaces PHPTemplate as the default templating engine. One of the results of this change is that all of the theme_* functions and PHPTemplate based *.tpl.php files have been replaced by *.html.twig template files.

Working With Twig Templates

Drupal allows you to override all of the templates that are used to produce HTML markup so that you can fully control the markup that is being output within a custom theme. There are templates for each page element ranging from the high level HTML to small fields.

Overriding templates

You can override Drupal core templates by adding templates to your theme folder that follow a specific naming convention.

To override templates you:

README Template

The following components and template are based on the input of many who contributed to the discussion "Create a README.txt template". For the sake of having a fleshed out example, we're using some sample text. Most of it comes from the Administration menu module's README.txt.

Table of contents

TOCs are optional, but appreciated for lengthy README files.

Discovering and Inspecting Variables in Twig Templates

Twig provides a dump function for debugging variables in templates.

The dump function outputs information about a template variable. The dump function will not display anything unless twig_debug is enabled.

To enable Twig debugging, find the twig.config parameters in your services.yml and set debug to true:

    debug: true

If your template has a title variable available, the following will dump its contents to your template:

{{ dump(title) }}

To dump all available variables and its contents in a template, add the following to your template:

{{ dump() }}

To dump only the available variable keys use:

{{ dump(_context|keys) }}

There are additional global variables available in all Twig templates:


Subscribe with RSS Subscribe to RSS - template