Code sur écran d'ordinateur

Symfony – debug a Twig template with dump()

Twig’s {{ dump() }} function is very handy to inspect a variable inside a template. But without the right package installed, the output is unreadable. Here is how to get a clean, collapsible display like this one:

Output of Twig's dump() function with Symfony's DebugBundle

Install the DebugBundle

The nice output comes from Symfony’s VarDumper component, enabled by the DebugBundle. Install it as a development dependency:

composer require --dev symfony/debug-bundle

With Symfony Flex, the debug alias also works:

composer require --dev debug

Using it in a template

Two syntaxes are available:

{# affiche le contenu d'une variable #}
{{ dump(user) }}

{# ou, sous forme de balise, plusieurs variables à la fois #}
{% dump user, articles %}

Good to know

Since the bundle is installed with --dev, the dump() function outputs nothing in production: no risk of leaking data if a call is left behind in a template.

See also