Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the function `dump` not exist in twig file?

Tags:

twig

symfony

I don't know why it shows this error

The function "dump" does not exist in twig file

while I have already writen in config.yml file:

services:
product_store.twig.extension.debug:
    class: Twig_Extension_Debug
    tags:
         - { name: 'twig.extension' }

and in twig file I try to dump with:

{{ dump(product) }}
like image 403
Kunwar Siddharth Singh Avatar asked Aug 28 '12 12:08

Kunwar Siddharth Singh


1 Answers

The answer from lifo encourages you to use the debug tag, but the debug tag {% debug product %} was deprecated in Twig 1.5 and replaced with the dump function {{ dump(product) }}.

The proper extension to enable as of Symfony Standard Edition 2.0.9 is Twig_Extension_Debug and should be added to app/config/config_dev.yml so it is only loaded in the dev environment:

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

You should then be able to use {{ dump(product) }} in your templates.

If a problem still exists, you can try a few things:

  1. Use php app/console container:debug twig.extension.debug --env=dev to ensure the dependency injection container is properly picking up your service definition.

    [container] Information for service twig.extension.debug
    
    Service Id       twig.extension.debug
    Class            Twig_Extension_Debug
    Tags
        - twig.extension                 ()
    Scope            container
    Public           yes
    Synthetic        no
    Required File    -
    
  2. Open the compiled dependency injection container class for your dev environment and search for Twig_Extension_Debug to see if any other service was already defined to use it. This file lives at app/cache/dev/appDevDebugProjectContainer.php

  3. Ensure the parameter %kernel.debug% is true.

  4. Ensure you're using Twig 1.5 or later.

like image 51
John Kary Avatar answered Oct 05 '22 06:10

John Kary