Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dump in TWIG?

Tags:

php

twig

symfony

I add for template (index.html.twig) simply:

{{ dump(product) }}

and i have error:

The function "dump" does not exist in AcmeStoreBundle:Default:index.html.twig at line 2 

Why this function is not enable, and how can i enable this?

like image 448
Tyler Greened Avatar asked Apr 12 '12 08:04

Tyler Greened


2 Answers

You need to configure the debugging extension:

# app/config/config.yml
services:
    acme_hello.twig.extension.debug:
        class:        Twig_Extension_Debug
        tags:
             - { name: 'twig.extension' }

Per the link mentioned above, Twig debugging is set to work by default in Symfony 2.5+ running Twig 1.16+, and the custom service definition is not necessary. See this answer for more details.

like image 175
meze Avatar answered Oct 22 '22 15:10

meze


When you configure it like @meze said, you can display all custom variables:

<h1>Variables passed to the view:</h1>
{% for key, value in _context %}
    {% if key starts with '_' %}
    {% else %}
        <pre style="background: #eee">{{ key }}</pre>
        {{ dump(value) }}
    {% endif %}
{% endfor %}

You can use my simple plugin to convenient inspect your variables:

Twig Dump Bar

like image 32
kapitalny Avatar answered Oct 22 '22 15:10

kapitalny