附加内容以阻止多个子模板

附加内容以阻止多个子模板

本文介绍了附加内容以阻止多个子模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何用树枝附加块有几个问题.答案总是使用继承和使用,然后调用 parent().不知何故,我不知道这在我的具体情况下是如何工作的:

base.html.twig

{% 块内容 %}{% endblock %}{% 块可附加 %}{% 端块 %}{% 阻止 another_appendable %}{% 端块 %}

site.html.twig

{% 扩展 base.html.twig %}{% 块内容 %}{# 这里使用/include/embed,我不知道#}{% 使用 sub1.html.twig %}{% 使用 sub2.html.twig %}{% endblock 内容 %}

sub1.html.twig

一些应该直接渲染的内容{% 块可附加 %}一些应该添加到可附加的东西{% 端块 %}{% 阻止 another_appendable %}此内容应添加到另一个可附加"{% 端块 %}

sub2.html.twig

{% 块可附加 %}应附加的其他内容{% 端块 %}

我希望 sub1 和 sub2 中的内容都呈现在可附加内容中.我怎样才能做到这一点?

解决方案

走吧.我遇到了同样的问题,这个解决方案对我有用:

base.html.twig

{% 块内容 %}{% endblock %}

site.html.twig

{% 扩展 base.html.twig %}{% 使用 sub1.html.twig 和 appendable 作为 appendableContent, another_appendable 作为 another_appendableContent %}{% 块内容 %}{% 块可附加 -%}{{块('appendableContent')}}{% 端块 %}{% 阻止 another_appendable -%}{{块('another_appendableContent')}}{% 端块 %}{% 端块 %}

sub1.html.twig

{% 使用 sub2.html.twig 和 appendable 作为 appendableContentAlternative %}{% 块可附加 %}一些应该添加到可附加的东西<br/><br/>{{ 块('appendableContentAlternative') }}{% 端块 %}{% 阻止 another_appendable %}此内容应添加到另一个可附加"中

sub2.html.twig

{% 块可附加 %}应该附加的附加内容<br/><br/>{% 端块 %}

根据我的研究,这种技术称为水平重用",文档如下:

http://twig.sensiolabs.org/doc/tags/use.htmlp>

there are several questions on how to append a block with twig. The answer is always using inheritance and use and then call the parent(). Somehow I don't know how this works in my specific case:

base.html.twig

{% block content %}{% endblock %}
{% block appendable %}
{% endblock %}
{% block another_appendable %}
{% endblock %}

site.html.twig

{% extends base.html.twig %}
{% block content %}
{# Here use/include/embed, i dont know #}
{% use sub1.html.twig %}
{% use sub2.html.twig %}
{% endblock content %}

sub1.html.twig

Some content that should be directly rendered
{% block appendable %}
some stuff that should be added to appendable
{% endblock %}
{% block another_appendable %}
This content should be added to "another appendable"
{% endblock %}

sub2.html.twig

{% block appendable %}
additional stuff that should be appended
{% endblock %}

I would like that both contents from sub1 and sub2 are rendered within appendable. How could I achieve that?

解决方案

Let's go. I had this same problem and this solution works for me:

base.html.twig

{% block content %}{% endblock %}

site.html.twig

{% extends base.html.twig %}

{% use sub1.html.twig with appendable as appendableContent, another_appendable as another_appendableContent %}

{% block content %}

    {% block appendable -%}
        {{ block('appendableContent') }}
    {% endblock %}

    {% block another_appendable -%}
        {{ block('another_appendableContent') }}
    {% endblock %}

{% endblock %}

sub1.html.twig

{% use sub2.html.twig with appendable as appendableContentAlternative %}

{% block appendable %}
    some stuff that should be added to appendable<br/><br/>

    {{ block('appendableContentAlternative') }}
{% endblock %}

{% block another_appendable %}
    This content should be added to "another appendable"<br/><br/>
{% endblock %}

sub2.html.twig

{% block appendable %}
    additional stuff that should be appended<br/><br/>
{% endblock %}

According my research this technique is called "horizontal reuse", and here's the doc:

http://twig.sensiolabs.org/doc/tags/use.html

这篇关于附加内容以阻止多个子模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:18