Twig 如何用包含的文件覆盖块

Twig 如何用包含的文件覆盖块,twig,Twig,我想从包含的细枝文件覆盖/扩展layout.twig中的块 我怎样才能做到这一点 原因是我将一些元素外包到不同的细枝文件中,以便能够反复使用它们。 这些细枝文件应在layout.twig中展开 我尝试了不同的解决方案: 嵌入标记仅覆盖包含文件中的块 使用标记不允许在layout.twig中扩展父块 也没有达到预期的效果 布局.细枝 <div style="color:red;"> {% block content %} <h1>content: layout<

我想从包含的细枝文件覆盖/扩展layout.twig中的块

我怎样才能做到这一点

原因是我将一些元素外包到不同的细枝文件中,以便能够反复使用它们。 这些细枝文件应在layout.twig中展开

我尝试了不同的解决方案:

嵌入标记仅覆盖包含文件中的块

使用标记不允许在layout.twig中扩展父块

也没有达到预期的效果

布局.细枝

<div style="color:red;">
{% block content %}
    <h1>content: layout</h1>
{% endblock %}
</div>

<div style="color:blue;">
{% block morecontent %}
    <h1>morecontent: layout</h1>
{% endblock %}
</div>
{% extends "layout.twig" %}

{% block content %}
    <h1>content: content</h1>

    {% include "include.twig" %}
    {% include "include2.twig" %}
{% endblock %}
<h1>content: include</h1>

{% block morecontent %}
    <h1>morecontent: include</h1>
{% endblock %}
<h1>content: include2</h1>

{% block morecontent %}
    <h1>morecontent: include2</h1>
{% endblock %}

{%block content%}
内容:布局
{%endblock%}
{%block morecontent%}
更多内容:布局
{%endblock%}
content.twig

<div style="color:red;">
{% block content %}
    <h1>content: layout</h1>
{% endblock %}
</div>

<div style="color:blue;">
{% block morecontent %}
    <h1>morecontent: layout</h1>
{% endblock %}
</div>
{% extends "layout.twig" %}

{% block content %}
    <h1>content: content</h1>

    {% include "include.twig" %}
    {% include "include2.twig" %}
{% endblock %}
<h1>content: include</h1>

{% block morecontent %}
    <h1>morecontent: include</h1>
{% endblock %}
<h1>content: include2</h1>

{% block morecontent %}
    <h1>morecontent: include2</h1>
{% endblock %}
{%extends“layout.twig”%}
{%block content%}
内容:内容
{%include“include.twig”%}
{%include“include2.twig”%}
{%endblock%}
包括.twig

<div style="color:red;">
{% block content %}
    <h1>content: layout</h1>
{% endblock %}
</div>

<div style="color:blue;">
{% block morecontent %}
    <h1>morecontent: layout</h1>
{% endblock %}
</div>
{% extends "layout.twig" %}

{% block content %}
    <h1>content: content</h1>

    {% include "include.twig" %}
    {% include "include2.twig" %}
{% endblock %}
<h1>content: include</h1>

{% block morecontent %}
    <h1>morecontent: include</h1>
{% endblock %}
<h1>content: include2</h1>

{% block morecontent %}
    <h1>morecontent: include2</h1>
{% endblock %}
内容:包括
{%block morecontent%}
更多内容:包括
{%endblock%}
包括2.树枝

<div style="color:red;">
{% block content %}
    <h1>content: layout</h1>
{% endblock %}
</div>

<div style="color:blue;">
{% block morecontent %}
    <h1>morecontent: layout</h1>
{% endblock %}
</div>
{% extends "layout.twig" %}

{% block content %}
    <h1>content: content</h1>

    {% include "include.twig" %}
    {% include "include2.twig" %}
{% endblock %}
<h1>content: include</h1>

{% block morecontent %}
    <h1>morecontent: include</h1>
{% endblock %}
<h1>content: include2</h1>

{% block morecontent %}
    <h1>morecontent: include2</h1>
{% endblock %}
内容:包括2
{%block morecontent%}
更多内容:包括2
{%endblock%}
这样,包含文件的“morecontent”将直接放在内容之后

<div style="color:red;">
    <h1>content: content</h1>
    <h1>content: include</h1>
    <h1>morecontent: include</h1>
    <h1>content: include2</h1>
    <h1>morecontent: include2</h1>
</div>
<div style="color:blue;">
    <h1>morecontent: layout</h1>
</div>

内容:内容
内容:包括
更多内容:包括
内容:包括2
更多内容:包括2
更多内容:布局
但我希望它们位于layout.twig的morecontent块中

 <div style="color:red;">
    <h1>content: content</h1>
    <h1>content: include</h1>
    <h1>content: include2</h1>
</div>
<div style="color:blue;">
    <h1>morecontent: layout</h1>
    <h1>morecontent: include</h1>
    <h1>morecontent: include2</h1>
</div>

内容:内容
内容:包括
内容:包括2
更多内容:布局
更多内容:包括
更多内容:包括2

您在这里是如何使用延迟扩展的?我建议用要在块中显示的includes填充数组,然后循环includesHi,我在{%block morecontent deferred%}中使用了它在layout.twig中。感谢您提示将其填充到数组中。这很有帮助!通过定义一个全局变量,我在其中附加html并在layout.twig中循环它!您的欢迎:)可惜这仍然不是
twig
中的一个功能。您是如何在这里使用延迟扩展的?我建议在数组中填充您想要添加的include的在块中展开,然后循环includesHi,我在layout.twig中的{%block morecontent deferred%}中使用了它。感谢您提示将其填充到数组中。这很有帮助!通过定义一个全局变量,我在其中附加html并在layout.twig中循环它!欢迎:)可惜这仍然不是
twig
中的功能