Symfony2 Debugging with Twig Extensions

There use to be a time when one could add twig extensions in this way:

extensions: [twig.extensions.debug]

However the newer way of adding extensions is as services using tags to let your setup know that these services are indeed twig.extensions and they need to be called when twig related services are executed. So we just need to drop this into our config.yml to make this work and also of course have the debug parameter on your config.yml set to true:

twig:
    debug:            %kernel.debug%

And here is the code for the extensions:

services:
   twig.extension.text:
       class: Twig_Extensions_Extension_Text
       tags:
           - { name: twig.extension }
   twig.extension.debug:
       class: Twig_Extensions_Extension_Debug
       tags:
           - { name: twig.extension }

And its usage according to documentation:

{% debug variabletodump %}

6 thoughts on “Symfony2 Debugging with Twig Extensions

      • Really? I’m using Symfony 2 and when attempting to use the var dump filter I am getting:

        The filter “var_dump” does not exist in ::.html.twig at line 19

        Are you sure you don’t have an extension for it?

  1. The Debug extension should not be available from the main config.yml file. Only activate it for dev and test environments, so you need to move the lines to the config_dev.yml and config_test.yml files.

  2. `The debug tag only works when the debug environment option is set to true`
    where i can set debug environment to true ?
    i already set config.yml , but i can’t use {% debug var %}
    err > Unknown tag name “debug” in {} at line 21

Leave a Reply

Your email address will not be published. Required fields are marked *