Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting included Twig templates?

Tags:

twig

symfony

I'd like to pass the output of an included Twig template to another included Twig template as a parameter, like so:

{% include 'MyBundle:Default:tpl1.html.twig' with {'item': include 'MyBundle:Default:tpl2.html.twig'} %}

Unfortunately, this does not work as the syntax is invalid. Any ideas how to nest templates like this / store the output of an included template in a variable? Or is there an alternative way to accomplish what I want to do? I thought about defining blocks in the included template, but it does not seem to be possible to overwrite them from the "outer" template ...

like image 774
Michael Sauter Avatar asked Oct 10 '11 13:10

Michael Sauter


People also ask

Is Twig a template engine?

Twig is a modern template engine for PHPSecure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.

How do you dump in Twig template?

In a Twig template, you can use the dump utility as a function or a tag: {% dump foo. bar %} is the way to go when the original template output shall not be modified: variables are not dumped inline, but in the web debug toolbar; on the contrary, {{ dump(foo.

How do I override a Twig file?

Overriding templatesLocate the template you want to override. Copy the template file from its core location into your theme folder. Rename the template according to the naming conventions in order to target a more specific subset of areas where the template is used. Modify the template according to your wish.


1 Answers

Try settings the template's content in a variable:

{% set content %}
     {% include 'foo' %}
{% endset %}

{% include 'bar' with {'item': content } %}

It should work.

like image 133
Alessandro Desantis Avatar answered Sep 28 '22 09:09

Alessandro Desantis