Accessing a variable inside Jinja2 includes

I include the template in the base template, which the template I am issuing is expanding. I set the variable in the direct template and try to use it in the included template. I would expect the following to deduce Active, but instead there is no deduction. Why header.htmldoesn’t he see the variable 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 %}
+4
source share
1 answer

This seems to be a bug, as stated at https://github.com/mitsuhiko/jinja2/issues/352 .

The workaround involves accessing the variable before turning it on.

base.html

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

Source: https://habr.com/ru/post/1614761/


All Articles