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.
source share