{% extends %} 与 {% block %} 到底怎么工作?super() 取的是什么?block 里为什么看不到循环变量?继承层数多了会不会慢?这篇用最小的模板把每个规则渲染一遍。
[base.html] <html>
<title>{% block title %}默认标题{% endblock %}</title>
<body>
{% block content %}基础内容{% endblock %}
<footer>{% block footer %}© base{% endblock %}</footer>
</body></html>
[child.html] {% extends "base.html" %}
{% block title %}子页面 - {{ super() }}{% endblock %}
{% block content %}<h1>{{ heading }}</h1>{% endblock %}
渲染 child.html → '<html>\n<title>子页面 - 默认标题</title>\n<body>\n<h1>你好</h1>\n<footer>© base</footer>\n</body></html>'上一篇:Flask 模板搜索路径;下一篇:Context Processor。
渲染 grand.html → '<html>\n<title>子页面 - 默认标题</title>\n<body>\n<h1>你好</h1><p>孙页面追加</p>\n<footer>孙页脚|© base</footer>\n</body></html>'
nested_child → '[外 内被覆盖]' 只有文字的子模板 → '<html>\n<title>默认标题</title>\n<body>\n基础内容\n<footer>© base</footer>\n</body></html>'
没有 scoped: '子 未定义|子 未定义|' scoped : '子 1|子 2|'
'<title>页面</title><h1>页面</h1>'
layout='alt_layout.html' → '<main>动态布局内容</main>' layout='base.html' → '<html>\n<title>默认标题</title>\n<body>\n动态布局内容\n<footer>© base</footer>\n</body></html>' layout 缺失 → UndefinedError: 'layout' is undefined
extends 前面有文字(不会报错,文字照样输出): 前面有文字\n<html>\n<title>默认标题</title>\n<body>\n基础内容\n<footer>© base</footer>\n</body></ht
子模板里 {% set %} 对父模板: '[子里 set]b:子里 set' (子模板顶层的 set 在父模板渲染前执行,父模板读到了它)
include 默认带 context;without context 看不到 v;ignore missing 跳过缺失: '主<1>|<无v>|'0 层继承:渲染 3.7 µs 3 层继承:渲染 9.7 µs 10 层继承:渲染 23.4 µs