Debugging Twig templates

Last updated on
22 December 2016

The Twig templating engine offers a debug tool.

The Drupal 8 implementation also adds an additional tool that allows you to locate the template that outputs the markup.

Enable debugging

You enable Twig Debugging in sites/default/services.yml.
(If services.yml does not yet exist; copy and rename it to services.yml.)

Set the debug variable to true.

    debug: true 

If you are using Firebug (in Firefox) make sure that "Show Comments" is enabled:

Firebug setting for twig debug

Automatic reloading

Twig templates are compiled to PHP classes on disk for better performance, but this means by default your templates are not refreshed when you make changes. Don't enable this in production.

To manually rebuild the templates run drush cr. To save time during development, enable automatic reloading by setting twig_config.auto_reload in services.yml (by default, auto reloading will turned on when twig_config.debug is true).

For more information, see

Printing variables

{{ dump() }}
{{ dump(var) }}

If you have Devel's kint submodule (install to require-dev with Composer using composer require --dev drupal/devel 1.0-alpha1 and install with Drush with drush -y en kint) you can get an accordion display of the variables available to twig with:

{{ kint() }}

More debugging options can be found in the next section.