Pass variables from child template to parent in Jinja2

前端 未结 2 856
予麋鹿
予麋鹿 2021-01-07 17:48

I want to have one parent template and many children templates with their own variables that they pass to the parent, like so:

parent.html:

{% block          


        
2条回答
  •  时光说笑
    2021-01-07 18:34

    Ah. Apparently they won't be defined when they are passed through blocks. The solution is to just remove the block tags and set it up like so:

    parent.html:

    {% if bool_var %}
        {{ option_a }}
    {% else %}
        {{ option_b }}
    {% endif %}
    

    child.html:

    {% extends "parent.html" %}
    
    {% set bool_var = True %}
    {% set option_a = 'Text specific to this child template' %}
    {% set option_b = 'More text specific to this child template' %}
    

提交回复
热议问题