关于如何用树枝附加块有几个问题.答案总是使用继承和使用,然后调用parent().不知怎的,我不知道这在我的具体情况下是如何工作的:
base.html.twig
{% block content %}{% endblock %}
{% block appendable %}
{% endblock %}
{% block another_appendable %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
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 %}
Run Code Online (Sandbox Code Playgroud)
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 %}
Run Code Online (Sandbox Code Playgroud)
sub2.html.twig
{% block appendable %}
additional stuff that should be appended
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
我希望sub1和sub2中的两个内容都在appendable中呈现.我怎么能实现这一目标?
我们走吧.我有同样的问题,这个解决方案对我有用:
base.html.twig
{% block content %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)
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 %}
Run Code Online (Sandbox Code Playgroud)
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 %}
Run Code Online (Sandbox Code Playgroud)
sub2.html.twig
{% block appendable %}
additional stuff that should be appended<br/><br/>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
根据我的研究,这种技术被称为"水平重用",这里的文档是:
http://twig.sensiolabs.org/doc/tags/use.html