Django模板中的嵌套块 [英] Nested blocks in Django templates
问题描述
我的Django应用程序中的master
模板如下所示:
The master
template in my Django app looks like this:
{% block parent %}
Some text...
{% block child %}
Default content here...
{% endblock child %}
...some more text
{% endblock parent %}
现在,应该以更改child
块的方式覆盖此模板:
Now, this template should be overwritten in a way that the child
block is changed:
{% extends "master.html" %}
{% block child %}
New content here...
{% endblock child%}
但是,渲染保持不变(在此处打印默认内容...").我是否错过了明显的东西或嵌套块是不可能的? (或者违反DRY原理,我是否要重新定义parent
块?)
However, the rendering stays the same (printing "default content here..."). Have I missed something obvious or are nested blocks not possible? (Or, violating the DRY principle, have I to re-define the parent
block?)
编辑:如果需要的话,我正在使用Django 1.1.
I'm working with Django 1.1, if that matters.
推荐答案
好的,这是Django模板系统中的错误.对于大多数其他情况,本·詹姆斯是对的(请参阅上面对我的问题的评论).
OK, it's a bug in Django's template system. For most other cases, Ben James is right (see his comment to my question above).
在我的情况下,child
块位于{% ifnotequal a b %}
块内部,而 that 破坏了该块的继承.我认为这是一个错误,因为有数十种此类操作的自然用例.
In my case, the child
block was inside a {% ifnotequal a b %}
block, and that breaks the block inheritance. I consider that to be a bug, since there are dozens of natural use cases for such operations.
这篇关于Django模板中的嵌套块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!