使用 parent() 函数并覆盖父级的子级块 [英] Using parent() function and overriding parent's children block
本文介绍了使用 parent() 函数并覆盖父级的子级块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使用 {{ parent() }} 并覆盖它的一个子块,我找到了一些解决方案,但看起来不对,这是一个可以解释问题的示例:
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?
推荐答案
哇,解决方案很简单,只需在包装块外面写块二,块没有层次结构:)
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 %}
这篇关于使用 parent() 函数并覆盖父级的子级块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文