12
votes

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.

3
looks to me like your environment is not setting correctly! Have you tried to output what 'app.environment' is in production? Changes are it will be set to dev :)Phorce
Hi there @Phorce. when I add <p>Application Environment: {{ app.environment }}</p> in my twig file, it shows "Application Environment: dev" for my "app_dev.php" link, and for my regular prod environment it shows "Application Environment: prod".Alvin Bunk

3 Answers

3
votes

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

3
votes

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) }}
1
votes

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 the dev and test environments, to avoid leaking sensitive information in production. In fact, trying to use the dump() function in the prod environment will result in a PHP error.

So either delete all dumps 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.