Last updated October 31, 2013. Created by Cottser on February 3, 2013.
Edited by Fabianx, sillygwailo. Log in to edit this page.

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, uncomment the following line in your settings.php (or add it if it's not there):

$settings['twig_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.

See Twig's dump function documentation to learn more about the dump function.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

If you want to see all variables, but dump() results in exhausted memory because of recursion or the like, you can loop through _context to see the all the keys in it:

<ol>
    {% for key, value in _context  %}
      <li>{{ key }}</li>
    {% endfor %}
</ol>

Then use a conditional to check (for example {{ if loop.index = 2 %}), and dump that value only if necessary.