9

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?

j0k
  • 22,600
  • 28
  • 79
  • 90
Tyler Greened
  • 115
  • 1
  • 2
  • 6
  • 1
    possible duplicate of [How to var_dump variables in twig templates?](http://stackoverflow.com/questions/7317438/how-to-var-dump-variables-in-twig-templates) – meze Apr 12 '12 at 08:54

3 Answers3

15

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.

Community
  • 1
  • 1
meze
  • 14,975
  • 4
  • 47
  • 52
  • I don't know if these are old answers or just incomplete, but this definitely does NOT work for me. I've tried many different versions of this answer, to no avail. – Chadwick Meyer Oct 01 '14 at 00:24
  • @ChadwickMeyer there's a link that says all you need is to enable it in the config `twig: debug: true` – meze Oct 01 '14 at 17:12
  • Thanks. I had done that and it didn't work. But I also upgraded from Symfony 2.4 to 2.5 and ran `composer update` (which upgraded from Twig 1.15 to 1.16 and now it's working. So who knows... – Chadwick Meyer Oct 01 '14 at 21:57
  • 1
    And for the record, I deleted the custom config you mention in this answer, since the link you reference says Twig debug enabled in dev environment by default. – Chadwick Meyer Oct 01 '14 at 21:58
  • 1
    @ChadwickMeyer don't know why your edit was rejected. Added your link and upvoted your answer. Thanks. – meze Oct 03 '14 at 09:18
2

Symfony 2.7+ UPDATE:

The DebugBundle allows greater integration of the component into the Symfony full-stack framework. It is enabled by default in the dev and test environment of the Symfony Standard Edition.

Check the VarDumper component and its integration on Twig.

OLD ANSWER:

I would like to suggest a non-native solution. You'll need a third-party bundle, but the final result will be great!

Improvements to the dump version:

  1. all dumps are styled
  2. you can provide the max nesting level to avoid memory issues with large objects
  3. very useful helpers like ldd(), which is an alias for the classic "dump and die"
  4. it has a console dumper (eg. php app/console ladybug:dump "Symfony\Component\HttpFoundation\Request")
  5. it provides an integration with the Symfony Profiler
  6. it automatically detects Symfony, Doctrine, Twig and Silex classes, and links them to the official documentation

Here the links:

Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
  • on Symfony 2.7+ you're probably better off with the official `VarDumper` component: http://symfony.com/doc/current/components/var_dumper.html#debugbundle-and-twig-integration – Francesco Casula Dec 06 '16 at 17:23
2

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

kapitalny
  • 310
  • 4
  • 7