简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM