0

我有这样的树枝文件层次结构

我的主要(用于控制器)树枝

{% extends "MainSiteBundle::layout.html.twig" %}

{% block footer_moderator_buttons %}
    some buttons
{% endblock %}

{% block content_body %}
    <p>hello moderator</p>

    {{ include ('MainBlogBundle:_parts:postList.html.twig', {'postList': aPostDraft}) }}

{% endblock %}

postList.html.twig

<div class="post-list">
{% for postSingle in postList %}
    {{ include ('MainBlogBundle:_parts:postSingle.html.twig', {'postSingle': postSingle}) }}
{% endfor %}
</div>

postSingle.html.twig

<div class="post">
    <div class="post-header">
        <a class="title" href="3">{{ postSingle.title }}</a>
    </div>
    <div class="post-meta">
        <div>Date: {{ postSingle.date|date('D M Y') }}</div>
        <div>Category: <a href="#">{{ postSingle.getCategory.getTitle }}</a></div>
        <div>Author: <a href="#">{{ postSingle.getUser.username }}</a>
        </div>
    </div>
    <div class="post-body">
        <div class="content">
            <img width="450" height="200" src="#">
            <div class="text">{{ postSingle.content }}</div>
        </div>
    </div>

    <div class="post-footer">
        {% block footer_moderator_buttons %}f{% endblock %}
        <div>Views: 152</div>
        <div>Comments: 1231</div>
        <div>
            <a class="link" href="#">More... </a>
        </div>
    </div>
</div>

如您所见,最后一个(postSingle.html.twig)有块“footer_moderator_buttons”,那么我该如何从主树枝(第一个)更改它?当前不工作,我需要改变\做什么?

4

3 回答 3

1

在 Twig 1.8 中有embed标签(http://twig.sensiolabs.org/doc/tags/embed.html)。

您将不得不删除该postList.html.twig文件或解决它。

{% embed "MainBlogBundle:_parts:postSingle.html.twig" with {'postSingle': postSingle} %}
    {% block footer_moderator_buttons %}
        custom buttons here
    {% endblock %}
{% endembed %}
于 2014-11-03T20:26:22.910 回答
0

这个怎么样:

{% extends "MainSiteBundle::layout.html.twig" %}
....
{% block footer_moderator_buttons %}
{{ parent() }}
{% endblock %}

woops 没有放父级.. {{ parent() }} 将从扩展树枝继承 {% block footer_moderator_buttons %}{% endblock %}。

于 2014-11-06T10:19:50.997 回答
0

所以,你的问题实际上是“我想了解\什么树枝可以”。嗯,这个问题的答案是:“它不能将块从“主”覆盖到较小的块。”

如果你想使用 twig,你必须停止以 php include() 的方式来创建新文件,然后一遍又一遍地在其中“PUT”组件,例如页眉、页脚、菜单等组件。

在 twig 中,您可以定义带有块的主 twig 文件,这些块可以想象为可以但不必被覆盖的空白空间。当然,这仍然意味着您可以将 postList.html.twig 作为包含在某些扩展 MainSiteBundle::layout.html.twig 的文件中。postSingle.html.twig 也是如此。

我认为你抓住了树枝的逻辑,除了不要试图从错误的一侧覆盖块 - 在这种情况下,从 MainSiteBundle::layout.html.twig 到它的较小部分。

于 2013-07-08T06:29:03.567 回答