Python Jinja2中的访问变量包括

Python Jinja2中的访问变量包括,python,flask,jinja2,Python,Flask,Jinja2,我在我的基础模板中包含一个模板,我渲染的模板将扩展该模板。我在直接模板中设置了一个变量,并尝试在包含的模板中使用它。我希望下面的代码输出Active,但是没有输出。为什么header.html看不到变量active main.py @app.route("/") def root(): return render_template("page.html") page.html {% set active = True %} {% extends "base.html" %} {% i

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

main.py

@app.route("/")
def root():
    return render_template("page.html")
page.html

{% set active = True %}
{% extends "base.html" %}
{% include "header.html" %}
{% if active %}Active{% endif %}
<!-- {{ active }} -->
{% include "header.html" %}
base.html

{% set active = True %}
{% extends "base.html" %}
{% include "header.html" %}
{% if active %}Active{% endif %}
<!-- {{ active }} -->
{% include "header.html" %}
header.html

{% set active = True %}
{% extends "base.html" %}
{% include "header.html" %}
{% if active %}Active{% endif %}
<!-- {{ active }} -->
{% include "header.html" %}

这似乎是一个bug,如中所述

解决方法包括在include之前访问变量

base.html

{% set active = True %}
{% extends "base.html" %}
{% include "header.html" %}
{% if active %}Active{% endif %}
<!-- {{ active }} -->
{% include "header.html" %}

{%include“header.html”%}
这是一个悬而未决的问题: