以 Flask学习笔记07之模板渲染 中的user.html 为例
首先创建一个base.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>模板</h1> {% block content %}{% endblock %} </body> </html>
新的user.html
{% extends "base.html" %} {% block content %} {{ users.0 }} {{ users[1] }} {#以字符串的形式展示,防止了xss攻击 #} {{ txt }} {#信任#} {{ txt| safe }} {{ html }} {{ func(8) }} {{ global_template(1,2) }} {#真是有点扯蛋#} {{ 1| filter_template(2,3) }} <hr/> {#if条件判断#} {% if 1|filter_template(2,3) %} <div>666</div> {% else %} <div>999</div> {% endif %} <hr> {# 通过include 导入form.html#} {% include "form.html" %} <hr> {# 定义一个宏, 宏默认是不会显示在页面上的, 想用的时候,调用这个宏函数就是#} {% macro macro_func(name,type='text', value= '') %} <h1>自定义一个宏</h1> <input type="{{ type }}" , value="{{ value }}" , name="{{ name }}"> <input type="submit" value="提交"> {% endmacro %} {# 调用宏,页面会显示#} {{ macro_func('username',value='第一次调用') }} {{ macro_func('nickName') }} {% endblock %}
include 导入的form.html
<input type="text" placeholder="用户名"> <input type="password" placeholder="密码">
页面展示
总结:
1. 使用extends 继承模板
2. 使用include 导入模板
3. 使用macro 重复调用模板
日拱一卒无有尽,功不唐捐终入海