2016-07-28 115 views
0

我需要在component.twig中包含child-1.twig和child-2.twig,并且在page.twig中包含component.twig。通过从Twig变量包含路径?

在我page.twig:

{% set items = [ 
    '{% include "child-1.twig" %}', 
    '{% include "child-2.twig" %}' 
] %} 

{% include "component.twig" with items %} 

在component.twig:

<div class="component"> 
    {% for item in items %} 
    {{ item }} 
    {% endfor %} 
</div> 

的复杂性来自于事实,我不能修改component.twig,只有page.twig。我上面的代码将工作,如果{% include "child-1.twig" %}{% include "child-2.twig" %}被呈现,但相反,他们被打印到页面上的一串文字。

我可以做一些类似于我的方法,但让孩子包括实际运行吗?

+0

为什么你就不能修改'component.twig '? – DarkBee

+0

该文件正由另一个应用程序在不同的实例中使用,它将包含其他子模板。 – Evans

回答

0

我可以建议你在该文件(component.twig

{% block includes %}{% endblock %} 

然后你就可以做到这一点添加一个空块:

{% embed "component.twig" with items %} 
    {% block includes %} 
     {% include "child-1.twig" %} 
     {% include "child-2.twig" %} 
    {% endblock %} 
{% endembed %}