gpt4 book ai didi

flask - 金贾 : How does the null-master fallback example work?

转载 作者:行者123 更新时间:2023-12-03 15:51:33 25 4
gpt4 key购买 nike

来自官方 jinja 站点的示例代码:

{% if not standalone %}{% extends 'master.html' %}{% endif -%}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>{% block title %}The Page Title{% endblock %}</title>
<link rel="stylesheet" href="style.css" type="text/css">
{% block body %}
<p>This is the page body.</p>
{% endblock %}

据我了解,当 standalone 为 true 时,将打印以下代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>{% block title %}The Page Title{% endblock %}</title>
<link rel="stylesheet" href="style.css" type="text/css">
{% block body %}
<p>This is the page body.</p>
{% endblock %}

当standalone 为false 时,打印:
{% if not standalone %}
<<master.html's code>>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>{% block title %}The Page Title{% endblock %}</title>
<link rel="stylesheet" href="style.css" type="text/css">
{% block body %}
<p>This is the page body.</p>
{% endblock %}

这似乎非常奇怪。我显然错过了一些明显的东西,它是什么?

最佳答案

从文档中没有立即清楚的是,当一个模板扩展另一个模板时,只会呈现子模板中在父模板中具有对应项的块。其他一切都被丢弃了。

所以在非独立模式下:

{% if not standalone %}
{% only care about blocks also in "master.html" %}
{% endif %}
{# Everything below is ignored #}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>{# The following will be rendered if master has a block named title. #}
{% block title %}The Page Title{% endblock %}
{# All the following will be ignored #}</title>
<link rel="stylesheet" href="style.css" type="text/css">
{# This *may* be rendered, if master.html has a block named "body" #}
{% block body %}
<p>This is the page body.</p>
{% endblock %}

关于flask - 金贾 : How does the null-master fallback example work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9547547/

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