文档结构:
- base.html
{% include tools.html %}- tools.html
{% block ... %}{% block ... %}- page.html
{% extends base.html %}
其中 ——
tools.html中含有一些{% block %}标签;tools.html被{% include %}标签引入到了base.html中;base.html被{% extends %}标签引入到了page.html中;
想要实现的效果:
在 page.html 中引用 tools.html 中的标签,进行改写。
遇到的问题:
page.html 能引用 tools.html 中的普通 html 代码,但是无法对 {% block %} 标签进行改写。
查到的资料:
Stack Overflow 上这个问题的第二个答案引用了一段 django 文档中的话:
The include tag should be considered as an implementation of "render this subtemplate and include the HTML", not as "parse this subtemplate and include its contents as if it were part of the parent". This means that there is no shared state between included templates -- each include is a completely independent rendering process.
大意是说 tool.html 在被 {% include %} 引入进 base.html 时就已经被渲染成 html 文本了?
那这样的话,有没有比较方便的替代方案?
补充说明:
tools.html 中的内容除了 base.html,还有其它地方会用到,所以就被单独写在一个文件里;
但是在其它地方引用的时候会有一点点细微的改动,所以又在其中加入了 {% block %} 标签。
有没有一种比较“优雅”的方式既能保障 tools.html 能被复用,又能比较方便地在复用时进行微调?