Moirae
Moirae

Reputation: 139

Symfony TWIG override block content of embedded twig

I have some twigs that are embedded in base twig. Embedded twigs contain blocks that I'd like to override in other twigs, which extend base. This changes are not showing. I saw similar questions, but couldn't deduce the answer from that.

For example in base twig:

<body>
    <div id="wrapper">
        {% embed 'Bundle::sidebar.html.twig' %}{% endembed %}
    </div>
</body>

Sidebar twig contains the block that should be overridden:

<div>Some content here</div>
{% block example_block %}
    Content of a block
{% endblock %}

Twig that extends the base:

{% extends 'Bundle::base.html.twig' %}
{% block example_block %}
    I want different content here
{% endblock %}

Upvotes: 0

Views: 803

Answers (1)

OrderAndChaos
OrderAndChaos

Reputation: 3860

Based on the Docs on embed http://twig.sensiolabs.org/doc/tags/embed.html I think this should work…

Base Twig Template:

<body>
    <div id="wrapper">
    {% block sidebar %}
        {% embed 'Bundle::sidebar.html.twig' %}{% endembed %}
    {% endblock %}
    </div>
</body>

Twig that extends base:

{% extends 'Bundle::base.html.twig' %}
{% block sidebar %}
    {% embed "Bundle::sidebar.html.twig" %}
    {# This block is defined in "sidebar.html.twig" #}
    {# and we override it right here: #}
        {% block example_block %}
            I want different content here
        {% endblock %}
    {% endembed %}
{% endblock %}

If you declare a sidebar block in the base template, then override it in the extended file, declaring the embed again and the blocks you want to override.

Upvotes: 3

Related Questions