Outputting PHP “var_dump” code debug capability inside Twig templates

Twig comes with its own Debug extension to allow users to output “var_dump()” — the function that spits out the raw values behind a PHP variable, array or object — inside a Twig template.  This functions is helpful in the dev environment while trying to debug code.

 

Step 1:  Add the following to the /app/config/config_dev.yml (don’t add this to config_prod.yml):

twig: 
    debug: 1
services:
    debug.twig.extension:
        class: Twig_Extensions_Extension_Debug
        tags: [{ name: 'twig.extension' }]

Step 2:  Inside any Twig template, you can “var_dump” variables using the Twig Debug Extension:

{% debug varName %} 

– Aaron Belchamber

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

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