Twig使用USE组合多个块

时间:2016-01-11 15:59:29

标签: symfony twig

我遇到了问题,我试图看看是否有人有解决方案。

我有一个扩展基础的树枝模板:

{% extends "base.html" %}

{% use "element1.html" %}
{% use "element2.html" %}

{% block css %}
{{ parent() }}
{% endblock %}

{% block body %}{% endblock %}

{% block javascript %}
{{ parent() }}
{% endblock %}

element1.html和element2.html几乎相同

{# element1.html #}

{% block css %}
some css...
{% endblock %}

{% block body %}
some body html    
{% endblock %}

{% block javascript %}
some javascript...
{% endblock %}

当代码运行时,element2会覆盖element1,有没有办法组合块,就像parent()将基本阻塞与主模板结合起来一样?

1 个答案:

答案 0 :(得分:2)

请适当地命名element1和element2的块,以便它不会覆盖另一个。

{% use "element1.html" with css as element1_css, body as element1_body, javascript as element1_js %}
{% use "element2.html" with css as element2_css, body as element2_body, javascript as element2_js %}

现在使用element1或element2中的适当块,例如

{% block element1_css %}{% endblock element1_css %}