Jinja2 中的访问变量包括
Access variable inside Jinja2 include
我在我的基本模板中包含一个模板,我呈现的模板对其进行了扩展。我在直接模板中设置了一个变量,并尝试在包含的模板中使用它。我希望以下输出 Active
,但没有输出。为什么header.html
看不到变量active
?
main.py
@app.route("/")
def root():
return render_template("page.html")
page.html
{% set active = True %}
{% extends "base.html" %}
base.html
{% include "header.html" %}
header.html
{% if active %}Active{% endif %}
这似乎是一个错误,如 https://github.com/mitsuhiko/jinja2/issues/352 所述。
解决方法是在包含之前访问变量。
base.html
<!-- {{ active }} -->
{% include "header.html" %}
我在我的基本模板中包含一个模板,我呈现的模板对其进行了扩展。我在直接模板中设置了一个变量,并尝试在包含的模板中使用它。我希望以下输出 Active
,但没有输出。为什么header.html
看不到变量active
?
main.py
@app.route("/")
def root():
return render_template("page.html")
page.html
{% set active = True %}
{% extends "base.html" %}
base.html
{% include "header.html" %}
header.html
{% if active %}Active{% endif %}
这似乎是一个错误,如 https://github.com/mitsuhiko/jinja2/issues/352 所述。
解决方法是在包含之前访问变量。
base.html
<!-- {{ active }} -->
{% include "header.html" %}