14
我想有一个父模板和许多孩子用自己的变量的模板,他们传递给家长,像这样:变量传给父母在Jinja2的
parent.html:
{% block variables %}
{% endblock %}
{% if bool_var %}
{{ option_a }}
{% else %}
{{ option_b }}
{% endif %}
child.html:
{% extends "parent.html" %}
{% block variables %}
{% set bool_var = True %}
{% set option_a = 'Text specific to this child template' %}
{% set option_b = 'More text specific to this child template' %}
{% endblock %}
但变量都未定义父。
我'parent.html '不直接我们e我的'bool_var',而是有一个'include'语句,它包含另一个使用'bool_var'的模板。在这个包含的模板中,该变量直到在'parent.html'文件中才出现undefined,或者使用了诸如“{{bool_var}}”之类的变量或者使用了重言式的“{%set bool_var = bool_var%}”。 – tremby