[Python] Trying to update context variable from included template in Jinja2

Discussão em 'Python' iniciado por Stack, Julho 31, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to update variable from an included template in Jinja 2

    base.j2:

    {% set ns = namespace(indent_lvl = 5) %}
    {% filter indent(width=4*ns.indent_lvl, first=True) %}
    base_indent_lvl: {{ns.indent_lvl}}
    {% include template_file with context %}
    {% endfilter %}


    template_file.j2:

    {% set ns.indent_lvl= ns.indent_lvl+1 %}
    template_indent_lvl: {{ns.indent_lvl}}


    So ns.indent_lvl increase when it enter in the template but is not save when it returning to base template. Is there a way to do it?

    Continue reading...

Compartilhe esta Página