It's possible to include a file in two different ways:
{% include 'fic.html.twig' %}
{{ include('fic.html.twig') }}
What is the difference between the two methods?
Source:
Tags are less flexible than functions, for example:
If you want to store contents of a file in a variable if you want to repeat it twice:
{% set content = include('test.twig') %}
Instead of:
{% set content %}
{% include 'test.twig' %}
{% endset %}
If you want to add filters:
{{ include('alert.twig') | upper }}
Its tag equivalent:
{% set temp %}
{% include 'alert.twig' %}
{% endset %}
{{ temp | upper }}
You see, {{ include }}
instead of {% include %}
will not change the world, but remove some complexity when you need to do tricky stuffs using Twig.
Also, according to the documentation, it is recommended to use {{ include() }}
to fit with best practices:
{{ }} is used to print the result of an expression evaluation;
{% %} is used to execute statements.
From Twig's changelog:
* 1.12.0-RC1 (2012-12-29)
* added an include function (does the same as the include tag but in a more flexible way)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With