Using parent() function and overriding parent'

2019-09-12 11:21发布

How can I use {{ parent() }} and override one of its children blocks, I found some solution but it looks wrong, here is an example that may be explain the issue:

index.html.twig:

{% block wrapper %}
    <h1>title</h1>
    {% block one %}<p>Content of block one</p>{% endblock one %}
    {% block two %}<p>Content of block one</p>{% endblock two %}
{% endblock wrapper %}

new_index.html.twig:

{% extends 'index.html.twig' %}
{% block wrapper %}
    {{ parent() }}
    {% block two %}<p>NEW content of block two</p>{% endblock two %}
{% endblock wrapper %}

But I see the content of block two twice (and it looks logical). How can I update the code to fix it?

标签: twig
1条回答
时光不老,我们不散
2楼-- · 2019-09-12 11:49

Wow, solution is simple, just write block two outside the wrapper block, there is no hierarchy for blocks :)

new_index.html.twig:

{% extends 'index.html.twig' %}
{% block wrapper %}
    {{ parent() }}
{% endblock wrapper %}
{% block two %}<p>NEW content of block two</p>{% endblock two %}
查看更多
登录 后发表回答