Twig's documentation for tag include looks very similar to that of function include.
Tag include:
{% include 'header.html' %}
Function include:
{{ include('template.html') }}
Can somebody point out in what circumstances, one is preferred over the other? Thanks!
There are two kinds of delimiters: {% ... %} and {{ ... }} . The first one is used to execute statements such as for-loops, the latter outputs the result of an expression.
The include statement includes a template and returns the rendered content of that file into the current namespace. The embed tag combines the behaviour of include and extends. It allows you to include another template's contents, just like include does.
Based on the differences, Include should be used only when you need to split a template into many functional sub-templates and reuse the wrapped code elsewhere. While Embed is used when you need to customise the reusable templates.
{{ include() }}
Was introduce in Symfony 2.2:
Using a function allows you to do whatever you want with the output (which is not possible with a tag), like a simple:
{{ set content = include('some_template') }}
But as Fabien Potentier (twig founder) said
the function and the tag does indeed the exact same thing
You can find the discution about it's introduction here: https://github.com/twigphp/Twig/pull/926
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