gpt4 book ai didi

jinja2 - 在 Jinja2 中将变量从子模板传递给父模板

转载 作者:行者123 更新时间:2023-12-03 12:45:16 24 4
gpt4 key购买 nike

我想要一个父模板和许多子模板,它们带有自己传递给父模板的变量,如下所示:

父.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 %}

但是变量最终在父级中未定义。

最佳答案

啊。显然,当它们通过块时不会被定义。解决方案是删除块标记并像这样设置它:

父.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' %}

关于jinja2 - 在 Jinja2 中将变量从子模板传递给父模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24833370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com