johnnyevolunium johnnyevolunium - 3 years ago 198
Twig Question

Difference between {% include '' %} and {{ include('') }} in Twig

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:


Answer Source

Tags are less flexible than functions, for example:

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

2) 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 looks recommanded to use {{ include() }} to fit with best practices :

{{ }} is used to print the result of an expression evaluation;
{% %} is used to execute statements.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download