Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

Find and check variables in Twig template

13/04/2025, by Ivan

Menu

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

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

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

Inspecting a Single Variable

If your template contains a title variable, the following will print its contents:

{{ dump(title) }}

Discovering All Available Template Variables

To print all available variables and their contents in a template (after enabling debugging):

{{ dump() }}

To print just the available variable keys, use:

{{ dump(_context|keys) }}

In all Twig templates, these additional global variables are available:

  • _self refers to the current template and contains extended template info (like compiled class name and Twig environment). _self is deprecated and removed in Twig 2.x.
  • _context refers to the current context and contains all variables passed to the template (from theme(), preprocess, or set in the template). {{ dump() }} is the same as {{ dump(_context) }}.
  • _charset refers to the current character set.

Beware of dump()

If you want to see all variables but dump() causes memory exhaustion or recursion, loop through _context keys instead:

    {% for key, value in _context %}
  1. {{ key }}
  2. {% endfor %}

Then use a conditional (e.g., {% if loop.index == 2 %}) to dump only what you need.

More Information

See Twig’s dump() function documentation for more info.

Debugging with Xdebug

The most recommended way to debug is using an IDE with an Xdebug plugin.

The easiest setup is using commercial PHPStorm and Xdebug. Microsoft’s VSCode is a free, open source IDE with Xdebug plugins that do the same without paid software.

Xdebug lets you step through execution, inspect variables, and avoid infinite loops that might occur with dump or kint.

A handy module called twig_xdebug can assist in showing available variables in Xdebug.

Debugging with Kint

The dump() function can clutter your page. A better alternative is Kint, a PHP debugger. The kint() function works like dump() but provides a collapsible/expandable interface.

The Devel module includes a Kint integration:

1. Download the Devel module
2. Enable the “devel kint” submodule
3. Just like dump(), kint() only works with debugging enabled. Enable Twig debugging
4. In your .twig files, use kint() to inspect variables
5. Optionally install the Search Kint module to search within nested variables

Whitelist Policy for Methods and Classes

When working with objects, only whitelisted getter/setter methods and classes are allowed via TwigSandboxPolicy. This prevents calling unintended methods in templates (e.g., {{ node.delete }} could delete a node without a whitelist in place). You can extend this whitelist in settings.php like this:

$settings['twig_sandbox_whitelisted_methods'] = [
  'id',
  'label',
  'bundle',
  'get',
  '__toString',
  'toString',
  'referencedEntities',
];

Where referencedEntities is the method you want to use in your Twig template.