The difference between {% include ''%} and {{include ('')}} in Twig

You can include a file in two ways:

{% include 'fic.html.twig' %} {{ include('fic.html.twig') }} 

What is the difference between the two methods?

A source:

+6
source share
4 answers

Labels are less flexible than functions, for example:

1) If you want to save the contents of the file in a variable, if you want to repeat it twice:

 {% set content = include('test.twig') %} 

Instead:

 {% 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 it will remove some complexity when you need to do complex things with Twig.

In addition, according to the documentation , it is recommended to use {{ include() }} to follow best practices:

 {{ }} is used to print the result of an expression evaluation; {% %} is used to execute statements. 
+9
source

From Twig 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) 
+3
source

I think the same functions, but while {% include '' %} is a tag, {{ include('') }} is a function. Perhaps if you want to overwrite a function, it might be easier than a tag.

0
source

Symfony 2.8 (LTS) documentation

2.3 The include () function is available with Symfony 2.3. Previously, the tag was {% include%}.

0
source

Source: https://habr.com/ru/post/970736/


All Articles