Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig tag include vs function include

Tags:

php

twig

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!

like image 615
Mr. 14 Avatar asked Mar 22 '13 08:03

Mr. 14


People also ask

What is the difference between {{ }} and {% %} in twig?

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.

What's the difference between include and embed?

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.

Why do we need the only keyword for include or embed?

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.


1 Answers

{{ 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

like image 114
0x1gene Avatar answered Oct 05 '22 01:10

0x1gene