Twig 将内容附加到块 [英] Twig Append Content to Block

查看:24
本文介绍了Twig 将内容附加到块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在树枝模板中,是否可以将内容附加到块?

In twig templating, is it possible to append content to a block?

例如,考虑下面的模板文件.

For example, consider the template files below.

layout.html.twig

layout.html.twig

<html>
<head>
    <style>
    {% block css %}{% endblock css %}
    </style>
</head>
<body>
{% block content %}{% endblock content %}
</body>
</html>

inner.html.twig

inner.html.twig

{% block css %} 
a { color: #fff; }
body { background: #f00; }
{% endblock css %}

{% block content %}

Some contents here...
{% include 'myWidget.html.twig' %}

{% endblock content %}

myWidget.html.twig

myWidget.html.twig

{% block css %}
div a { color: #777; }
{% endblock css %}
{% block content %}
<div><a>myWidget content here...</a></div>
{% endblock content %}

注意块 css.. 我想要完成的是我希望将块 css 的每个内容附加到 layout.html.twig 的 css 块.因此,最终结果应该是:

Notice the block css.. What I am trying to accomplish is that I want to have each content of the block css appended to the layout.html.twig's css block. Thus, the end result should be:

<html>
<head>
    <style>
    a { color: #fff; }
    body { background: #f00; }
    div a { color: #777; }
    </style>
</head>
<body>
Some contents here...
<div><a>myWidget content here...</a></div>
</body>
</html>

推荐答案

这应该可以解决问题:

{% block css %}
    {{ parent() }}
    div a { color: #777; }
{% endblock css %}

{% block content %}
    <div><a>myWidget content here...</a></div>
{% endblock content %}

这篇关于Twig 将内容附加到块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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