Jinja:null-master回退示例如何工作? [英] Jinja: How does the null-master fallback example work?

查看:116
本文介绍了Jinja:null-master回退示例如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方jinja网站的示例代码:

  {%if not standalone%} {%extends'master.html '%} {%endif  - %} 
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 Transitional // EN>
< title> {%block title%}页面标题{%endblock%}< / title>
< link rel =stylesheethref =style.csstype =text / css>
{%block body%}
< p>这是网页正文。< / p>
{%endblock%}

据我所知,当standalone为true时,下面的代码:

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 Transitional // EN> 
< title> {%block title%}页面标题{%endblock%}< / title>
< link rel =stylesheethref =style.csstype =text / css>
{%block body%}
< p>这是网页正文。< / p>
{%endblock%}

当独立为false时, p>

  {%if not standalone%} 
<<<<< master.html的代码>>
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 Transitional // EN>
< title> {%block title%}页面标题{%endblock%}< / title>
< link rel =stylesheethref =style.csstype =text / css>
{%block body%}
< p>这是网页正文。< / p>
{%endblock%}

这看起来很奇怪。我明显错过了一些明显的东西,这是什么东西?

解决方案

模板扩展了另一个模板,只呈现子模板中具有父模板中对应部分的块。所有其他东西都被丢弃了。

所以在非独立模式下:

$ p $ {%if not standalone%}
{%只关心master.html%}
{%endif%}
{#下面的所有内容都被忽略#}
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 Transitional // EN>
< title> {#如果master有一个名为title的块, #}
{%block title%}页面标题{%endblock%}
{#以下全部内容将被忽略#}< / title>
< link rel =stylesheethref =style.csstype =text / css>
{#如果master.html有一个名为body的块,那么这个*可能会被渲染出来。}}
{%block body%}
< p>这是页面主体。 < / p为H.
{%endblock%}


The example code from the official jinja site:

{% 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 %}

As I understand it, when standalone is true the following code is printed :

<!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 %}

And when standalone is false, this is printed :

{% 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 %}

This seems is very weird. I am clearly missing something obvious, what is it ?

解决方案

The thing that is not immediately clear from the documentation is that when a template extends another template only the blocks in the child template that have a counterpart in the parent template(s) are rendered. Everything else is dropped.

So in non-standalone mode:

{% 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 %}

这篇关于Jinja:null-master回退示例如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆