【问题标题】:Access variable inside Jinja2 includeJinja2 中的访问变量包括
【发布时间】:2015-11-06 15:09:02
【问题描述】:

我在我的基本模板中包含一个模板,我渲染的模板扩展了该模板。我在直接模板中设置了一个变量,并尝试在包含的模板中使用它。我希望以下输出Active,但没有输出。为什么header.html 看不到变量active

ma​​in.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 %}

【问题讨论】:

标签: python flask jinja2


【解决方案1】:

这似乎是一个错误,如https://github.com/mitsuhiko/jinja2/issues/352 所述。

一种解决方法是在包含之前访问变量。

base.html

<!-- {{ active }} -->
{% include "header.html" %}

【讨论】:

    猜你喜欢
    • 2017-12-24
    • 2020-03-28
    • 2018-03-24
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 2017-12-08
    • 2017-06-25
    • 1970-01-01
    相关资源
    最近更新 更多