Discovering and Inspecting Variables in Twig Templates

Last updated on
9 February 2017

When working with a Twig template file most variables are documented in the comments for the template file. However, when they are not, or when themes or modules introduce new variables we need a way to discover all the variables available within the scope of a template. Twig provides the dump() function for discovering and inspecting variables in template files.

The dump() function will not display any output unless debugging is enabled. Learn how to enable Twig debugging.

Once enabled, the dump() function can be used to output information about a variable or variables within a template.

Inspecting a single variable

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

{{ dump(title) }}

Discovering all available variables in a template

To dump all available variables and their contents in a template, add the following to your template (after enabling debugging):

{{ 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. _self was deprecated and removed from Twig version 2.x.
  • _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 character set.

Beware of dump()

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 all the keys in it:

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

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

More information

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

Debugging with xdebug

The most often recommended way to debug is to use an IDE with an xdebug plugin.

The simplest configuration to setup is to use PHPstorm (commercial) and xdebug. Microsoft's VSCode is a free open-source IDE with plugins that will do the same but do not require you to buy commercial software.

Using xdebug will allow you to step through the process, view the variable contents, and do so in a way that eliminates the mysteries under the covers, as well as preventing the infinite loops that may arise when using dump or kint.

A helpful little module called twig_xdebug may help in showing available variables in with xdebug.

Debugging with kint

Twig's dump() function will often print a lot of text to the web page making it difficult to find the needle you want amidst the haystack of text.

An alternative to dump() is kint, a PHP debugging tool. The kint() function works exactly same as the dump() function above, but provides an expandable/collapsible interface to the variables that it prints out.

The devel project provides a devel kint module that allows themers to use kint() instead of dump().

  1. Download the devel module.
  2. Install the “devel kint” module.
  3. Like the dump function, kint() will not display any output unless debugging is enabled. Learn how to enable Twig debugging.
  4. In your .twig files, use kint() to inspect the variables just like dump() is described above.
  5. Optionally, download and install the Search Kint module to search the nested variable list.

Method and class whitelist policy

When working with objects, we've whitelisted calling certain set of getters and setters or objects through the TwigSandboxPolicy class. This is done to prevent accidental methods from being called in your templates. For example: {{ node.delete }} on the node object may delete the node if the whitelist was not in place.  You can extend this whitelist by adding the following to your settings.php

$settings['twig_sandbox_whitelisted_methods'] = [

Where 'referencedEntities' is the method you'd like to use in your template.