在Tornado中可以包含一个扩展其他模板的模板吗?
我在尝试包含一个模板时遇到了错误,这个模板是基于另一个模板的扩展……我不确定这是不被支持的情况,还是我做错了什么,因为这看起来是一个非常常见的场景。
我写的最简单的代码来重现这个错误是这样的:
test.py
import tornado.template
loader = tornado.template.Loader(".")
templ = loader.load("t1.html")
t1.html
{% include "t2.html" %}
t2.html
{% extends "t3.html" %}
t3.html
{# empty #}
当我运行 test.py
时,我在 tornado 的 template.py
中遇到了一个 NotImplementedError
的错误。
我是不是漏掉了什么,还是说这是个bug?
1 个回答
5
哦,抱歉,我刚才太专注于缺失的代码块了。
你描述的情况用 {% include %}
是不行的,但我用 {% module Template('t2.html', **args) %}
就可以,这样可以在自己的命名空间中渲染模板。模块的设置是由 tornado.web.Application
自动完成的,但在你给的例子中,最简单的模板加载器并没有做到这一点。
这个限制似乎是因为 {% extends %}
标签的实现方式造成的。