2

在 Tornado 中,您可以扩展模板,这是一个很棒的功能。然而,有时我想将一个变量传递给我正在扩展的模板。这似乎不会自动工作 - 有没有办法做到这一点?

例子

--file.py--
render_string("Foo.html",text="Hello World")


--Foo.html--
{% extends Bar.html %}
    {%block b1 %}
    Hi Mom.
    {% end %}
{% end %}

--Bar.html--
This is a triumph.
I'm making a note here -- {{text}}
{% block b1 %}
Also, this
{% end %}

如何将文本向上传递扩展,以便 Bar.html 可以理解?

4

1 回答 1

2

When you use self.render() or self.render_string(), tornado template engine find the first extended page and comes forward.

And, when you pass a variable to these functions, it's available from first to last templates have been extended. So, your code is correct and haven't problem.

(And also, you should using {% extends "Bar.html" %} without {% end %} statement for it.)

于 2015-04-16T14:47:41.450 回答