One of the goals of Twig is to limit the amount of logic in your templates.
Unfortunately you won't be able to do all sorts of stuff you can do with PHP.
To regain that power you can use TwigEvalExtension. Just write your php code
inside an eval()
template function and it gets passed to PHP's eval and you
will get the result.
You can install the extension by using composer require.
Using the eval function:
{{ eval('echo 1+1;') }}
Using the eval tag:
{% eval %}
echo file_get_contents("/var/log/system.log");
{% endeval %}
Just run
$ vendor/bin/phpunit tests/EvalExtensionTest.php
No pesky phpunit.xml needed.
Please don't.
It's eval in your templates. What could possibly go wrong?!