包含文件中的块不被扩展模板填充 [英] Blocks in included files not being filled by extended templates
问题描述
{%includebase / top.htmlwith context%}
{%includebase / nav.htmlwith context%}
< div id =content>
Stuff
{%block content%} {%endblock%}
< / div>
{%includebase / bottom.htmlwith context%}
base / nav.html
和 base / bottom.html
包含静态内容,但是 base / top.html
包含 {%block title%}
。所以,当我有第二个模板,试图从第一个文件继承像这样:
{%extends firstfile.html% }
pre>
{%block title%} Imarealpage!{%endblock%}
{%block content%} Lorem ipsum dorem smitshm {%endblock%}
解决方案
{%block title%} 部分不会呈现。如何确保它和包含文件中的任何其他块以及在扩展模板中定义的块是否应该被渲染?
你误解了 {%include%}
是如何工作的。 {%include%}
标记不是预处理器;在渲染之前,不会将包含的模板的代码直接放到包含的模板中。相反, {%include%}
会引发包含模板的新的独立模板渲染(就像直接从您自己的代码中渲染包含的模板一样),然后将呈现的结果包含到所包含模板的呈现中。
这意味着包含的模板有一个完全独立的继承层次结构,他们包括模板。例如,你可以有一个基地 component.html
模板,其中有一些块,然后有例如。 foo-component.html
以 {%extendscomponent.html%}
开头,并填入 component.html
。然后你可以有一个 layout.html
来做 {%includefoo-component.html%}
,并且会呈现 foo-component.html
,并继承 component.html
,并将结果放入这个位置在 layout.html
中。但是 layout.html
和 component.html
中的任何块之间的关系是零关系 - 它们是分开的使用单独的块结构和继承层次结构呈现。
I have a template that looks like this:
{% include "base/top.html" with context %}
{% include "base/nav.html" with context %}
<div id="content">
Stuff
{% block content %}{% endblock %}
</div>
{% include "base/bottom.html" with context %}
base/nav.html
and base/bottom.html
contain static content, but base/top.html
contains a {% block title %}
. So when I have a second template as that attempts to inherit from the first file like so:
{% extends firstfile.html %}
{% block title %}Imarealpage!{% endblock %}
{% block content %}Lorem ipsum dorem smitshm{% endblock %}
The {% block title %}
section isn't rendered. How do ensure that it, and any other blocks in included files and defined in extended templates are rendered as they should be?
You're misunderstanding how {% include %}
works. The {% include %}
tag is not a pre-processor; it doesn't plop the included template's code directly into the including template before rendering. Instead, {% include %}
fires off a new independent template render of the included template (just like as if you had rendered the included template directly from your own code), and then includes the rendered results into the rendering of the included template.
The implication of this is that included templates have a totally separate inheritance hierarchy from their including template. You can, for instance, have a base component.html
template with some blocks in it, and then have e.g. foo-component.html
which starts with {% extends "component.html" %}
and fills in some blocks from component.html
. And then you can have a layout.html
that does {% include "foo-component.html" %}
, and that will render foo-component.html
, complete with its inheritance of component.html
, and place the result into that spot in layout.html
. But there is zero relationship between any blocks in layout.html
and any blocks in component.html
-- they are separate renders with separate block structures and inheritance hierarchies.
这篇关于包含文件中的块不被扩展模板填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!