附加内容以阻止来自多个子模板的内容 [英] Append content to block from multiple subtemplates

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

问题描述

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

base.html.twig

{% block content %}{% endblock %}{% 块可追加 %}{% 结束块 %}{% 阻止 another_appendable %}{% 结束块 %}

site.html.twig

{% 扩展 base.html.twig %}{% 块内容 %}{#这里使用/包含/嵌入,我不知道#}{% 使用 sub1.html.twig %}{% 使用 sub2.html.twig %}{% 端块内容 %}

sub1.html.twig

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

sub2.html.twig

{% 块可追加 %}应该附加的额外内容{% 结束块 %}

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

解决方案

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

base.html.twig

{% block content %}{% endblock %}

site.html.twig

{% 扩展 base.html.twig %}{% 使用 sub1.html.twig with appendable as appendableContent, another_appendable as another_appendableContent %}{% 块内容 %}{% 块可追加 -%}{{ 块('appendableContent')}}{% 结束块 %}{% 阻止 another_appendable -%}{{ 块('another_appendableContent')}}{% 结束块 %}{% 结束块 %}

sub1.html.twig

{% use sub2.html.twig with appendable as appendableContentAlternative %}{% 块可追加 %}一些应该添加到可追加的东西

{{ 块('appendableContentAlternative')}}{% 结束块 %}{% 阻止 another_appendable %}此内容应添加到另一个可附加内容"

{% 结束块 %}

sub2.html.twig

{% 块可追加 %}应该附加的额外内容<br/><br/>{% 结束块 %}

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

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆