Reputation: 7764
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.
Upvotes: 15
Views: 19999
Reputation: 8540
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 doesn't exist, even if it's inside an unreachable if
block. So just like in your question.
Actually, Symfony's documentation of dumping says the same:
By design, the
dump()
function is only available in thedev
andtest
environments, to avoid leaking sensitive information in production. In fact, trying to use thedump()
function in theprod
environment will result in a PHP error.
So either delete all dump
s from your Twig files or create a workaround.
I would except dump
to do nothing in production environments – so I would create a custom Twig Function named dump
that doesn't return anything.
Sadly, I don't know in what location of your codebase you should add a new function to be used only in production environments. But here's 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 outputs nothing but also doesn't throw exceptions.
Upvotes: 1
Reputation: 125
The error in prod environment appears because the dump call is not available.
But you don't need to set debug to true, because you usually don't want to do this in the prod environment.
There is a very simple and much better workaround for this issue.
Instead of calling dump()
directly in the if block just include a separate twig file which contains the dump()
call.
change:
{% if app.environment == 'dev' %}
{{ dump(foo) }}
{% endif %}
into:
{% if app.environment == 'dev' %}
{% include 'dump.html.twig' %}
{% endif %}
dump.html.twig content:
{{ dump(foo) }}
Upvotes: 3
Reputation: 39390
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
Upvotes: 3