user1954544
user1954544

Reputation: 1687

Symfony2, Twig, change twig after include

I have such hierarchy of twig files

my main (for controller) twig

{% 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>

As you can see last (postSingle.html.twig) has block "footer_moderator_buttons", so how can i change it from main twig (first one) ? Current is not working, what I need change \ do ?

Upvotes: 0

Views: 998

Answers (3)

KaraKondzula
KaraKondzula

Reputation: 1

how about this:

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

woops didnt put parent.. the {{ parent() }} will inherit {% block footer_moderator_buttons %}{% endblock %} from extended twig.

Upvotes: 0

Jonwd
Jonwd

Reputation: 655

In Twig 1.8 there is embed tag (http://twig.sensiolabs.org/doc/tags/embed.html).

You would have to remove the postList.html.twig file or work around it.

{% embed "MainBlogBundle:_parts:postSingle.html.twig" with {'postSingle': postSingle} %}
    {% block footer_moderator_buttons %}
        custom buttons here
    {% endblock %}
{% endembed %}

Upvotes: 1

edo.schatz
edo.schatz

Reputation: 99

So, you question is actually "I want to understand how\what twig can". Well, the answer to that question is: "It can't overwrite blocks from "main" to the smaller ones."

If you want to use twig, you have to stop thinking in php include() way where you make new files and you "PUT" components in them over and over again, components such as header, footer, menu etc.

In twig, you define main twig file with blocks which can be imagined as empty spaces which can, but do not have to, be overwritten. Surely, it still means you can have postList.html.twig as include in some file that extends MainSiteBundle::layout.html.twig. The same goes for postSingle.html.twig.

I think you catch the logic of twig except don't try to overwrite blocks from the wrong side - in this case, from MainSiteBundle::layout.html.twig to it's smaller portions.

Upvotes: 0

Related Questions