Skip to content

Instantly share code, notes, and snippets.

@robdecker
Last active October 18, 2019 19:14
Show Gist options
  • Save robdecker/3d5b5de0a1fe1caf2a25 to your computer and use it in GitHub Desktop.
Save robdecker/3d5b5de0a1fe1caf2a25 to your computer and use it in GitHub Desktop.
[Twig debugging] See https://www.drupal.org/node/1906780 #d8 #twig

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:

parameters:
  twig.config:
    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:

  • _self references the current template and contains advanced information about a template, i.e. the compiled template class name and information about the Twig environment.
  • _context references the current context and contains all variables passed to the template such as variables sent from theme(), prepared by preprocess, or set in the template. Adding {{ dump() }} without specifying a variable is equivalent to {{ dump(_context) }}.
  • _charset references the current charset.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment