Why does the twig pattern throw an unknown dump function when using an if statement for the dev environment?

I have a branch template using Symfony3, like 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: Unaccepted PHP exception Twig_Error_Syntax: "Unknown" dump "in" search / search_pet_results.html.twig "on 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 \ "in \ "search / search_pet_results.html.twig \" on line 13. in /var/www/html/petition/vendor/twig/twig/lib/Twig/ExpressionParser.php∗73)} []

Any suggestions for my branch template? I do not know what to try, because it is "supposed" to work.

+6
source share
3 answers

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

So, probably your config.yml will be the same as the following:

config.yml

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

Try changing as shown below to enable it in all environments:

config.yml

 # Twig Configuration twig: debug: true 

Hope for this help

+3
source

An error in the prod environment appears because the dump call is not available.

But you do not need to set debug to true, because you usually do not want to do this in a prod environment.
There is a very simple and promising solution to this problem.

Instead of calling dump() directly in the if block, simply include a separate twig file that contains the dump() call.

changes:

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

in

 {% if app.environment == 'dev' %} {% include 'dump.html.twig' %} {% endif %} 

dump.html.twig content:

 {{ dump(foo) }} 
+1
source

This is similar to the question Check if a custom Twig function exists and then call it , which I recently answered. I found that Twig throws a Twig_Error_Syntax exception when trying to call a function that does not exist, even if it is inside an unreachable if block. So, as in your question.

In fact, the Symfony dumping documentation says the same thing:

By design, the dump() function is only available in dev and test to avoid confidential information leaking into production. In fact, trying to use the dump() function in a prod environment will result in a PHP error.

So, either remove all dump from your Twig files, or create a workaround.

I would like dump do nothing in production environments - so I would create a custom Twig function called dump that returns nothing . Edditor's answer may also work, but creating separate files for each dump call seems rather cumbersome, at least if you used dump in several places.

Unfortunately, I do not know where in your code base you should add a new function that will only be used in production environments. But here is the beef:

 $twig = new Twig_Environment(/* ... */); // Pseudo code: check environment if ($environment !== 'dev' && $environment !== 'test') { $twig->addFunction(new Twig_Function('dump', function() { return null; })); } // Or you can also check whether the `dump` function already exists if ($twig->getFunction('dump') === false) { $twig->addFunction(new Twig_Function('dump', function() { return null; })); } 

Then you can safely use dump in all environments; in production environments, it simply does not throw anything, but also does not throw exceptions.

0
source

All Articles