Symfony – Twig: debugging

When using PHP, you can use var_dump() if you need to quickly find the value of a variable passed. This is useful, for example, inside your controller. The same can be achieved when using Twig thanks to the debug extension. Template parameters can then be dumped using the dump function:

{{ dump(articles) }}

Each Twig template is compiled down to a native PHP class that is rendered at runtime. The compiled classes are located in the app/cache/{environment}/twig directory and in some cases can be useful while debugging. When debug mode is enabled (common in the dev environment), a Twig template will be automatically recompiled when changes are made to it. When debug mode is disabled (common in the prod environment), however, you must clear the Twig cache directory so that the Twig templates will regenerate.

Leave a Reply