Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does twig template throw unknown "dump" function when using if statement for 'dev' environment?

Tags:

twig

symfony

I have a twig template using Symfony3 like the follwing:

{% if app.environment == 'dev' %}
    {{ dump(students) }}
{% endif %}

But in the 'prod' environment it throws this error, shown in the var/logs/prod.log file:

[2016-05-18 21:28:28] request.CRITICAL: Uncaught PHP Exception Twig_Error_Syntax: "Unknown "dump" function in "search/search_pet_results.html.twig" at line 13." at /var/www/html/petition/vendor/twig/twig/lib/Twig/ExpressionParser.php line 573 {"exception":"[object] (Twig_Error_Syntax(code: 0): Unknown \"dump\" function in \"search/search_pet_results.html.twig\" at line 13. at /var/www/html/petition/vendor/twig/twig/lib/Twig/ExpressionParser.php:573)"} []

Any suggestions for my twig template? Don't know what to try, because this is "supposed" to work.

like image 214
Alvin Bunk Avatar asked May 19 '16 04:05

Alvin Bunk


People also ask

How do you dump in Twig template?

In a Twig template, you can use the dump utility as a function or a tag: {% dump foo. bar %} is the way to go when the original template output shall not be modified: variables are not dumped inline, but in the web debug toolbar; on the contrary, {{ dump(foo.

What are the features provided by Twig?

Twig is a modern template engine for PHP Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum. Secure: Twig has a sandbox mode to evaluate untrusted template code.

What is Twig environment?

Twig uses a central object called the environment (of class \Twig\Environment ). Instances of this class are used to store the configuration and extensions, and are used to load templates. Most applications create one \Twig\Environment object on application initialization and use that to load templates.


1 Answers

The dump function is not available by default, as described in the doc here . You must set the debug flag to true in order to enable on the environment. The flag is located in the config.yml files, under the twig section. Usually the value is taken from the kernel value.

So probably your config.yml is same as follow:

config.yml

# Twig Configuration
twig:
    debug:            "%kernel.debug%"

Try modify as follow in order to enable in all the environment:

config.yml

# Twig Configuration
twig:
    debug:            true

Hope this help

like image 94
Matteo Avatar answered Dec 08 '22 07:12

Matteo