模板引擎
- 模板渲染
- 变量
- 流程控制
1. 模板渲染
- Jinja2 模板引擎
- 页面渲染流程
一个简单的例子:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/user/<uname>/')
def user(uname):
return render_template("user.html", uname=uname)
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>hi shaun</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>user</title>
</head>
<body>
<h1>Hello, {{ uname }}</h1>
</body>
</html>
2. 变量
-
变量类型
- 字符串
- 列表元组
- 字典
- 复杂的对象
- 过滤器
from flask import Flask, render_template
app = Flask(__name__)
class UserModel(object):
name = "zhangsan"
age = 25
def __str__(self):
return "%s, %d" % (self.name, self.age)
@app.route('/')
def index():
var_str = "这是一个字符串"
var_str_en = "this is a string"
var_html = "<h1>this is a html</h1>"
var_list = ['zheng','shuang']
var_tuple = ('a', 'b', 'c')
var_dict = {'key1': 'value1', 'key2': 'value2'}
var_user = UserModel()
return render_template('var/index.html',
var_str = var_str,
var_str_en = var_str_en,
var_html = var_html,
var_list = var_list,
var_tuple = var_tuple,
var_dict= var_dict,
var_user = var_user
)
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>var_test</title>
</head>
<body>
<!-- 选择特定字符 -->
{{ var_str[2] }} {{ var_str[:2] }}
<hr>
<!-- 过滤器 将小写转成大写 -->
{{ var_str_en | upper }}
<hr>
<!-- 过滤器 不展示HTML形式 -->
{{ var_html | safe }}
<hr>
{{ var_list[1] }}
<hr>
{{ var_tuple[1] }}
<hr>
<!-- 字典拿对应值的两种方式 -->
{{ var_dict.key2 }} {{ var_dict["key1"] }}
<hr>
<!-- 对象如何拿对应值 -->
{{ var_user.name }}
<hr>
{{ var_user}}
</body>
</html>
3. 流程控制
- for 循环标签
- if 条件标签
- 页面复用
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
var_list = ['zheng','shuang']
var_dict = {'key1': 'value1', 'key2': 'value2'}
return render_template('label/test_label.html',
var_list = var_list,
var_dict = var_dict
)
@app.route('/user/')
@app.route('/user/<uname>/')
def user(uname=None):
return render_template('label/if.html', uname=uname)
@app.route('/extens/')
def extends():
return render_template('label/extens.html')
if __name__ == '__main__':
app.run(debug=True)
test_label.html 主要主要对字典的操作,以及计数操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test_label</title>
</head>
<body>
<h1>循环操作</h1>
<ul>
<!-- 使用loop.index 从1开始计数,使用loop.index0 从0开始计数 -->
{% for item in var_list %}
<li>{{ loop.index0}} {{ item }}</li>
{% endfor %}
<!-- 字典的操作方式 -->
{% for key, value in var_dict.items() %}
<li>{{ loop.index0}} {{ key }} -> {{ value }}</li>
{% endfor %}
</ul>
</body>
</html>
if.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>if</title>
</head>
<body>
<h1>条件操作</h1>
<ul>
{% if uname %}
<b>你好,{{ uname }} !</b>
{% else %}
<b>你是谁?</b>
{% endif %}
</ul>
</body>
</html>
base.html 主要注意 include
包含操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{% block title %}
<title>base page</title>
{% endblock %}
</head>
<body>
<header>
<h1>header</h1>
</header>
<article>
{% block content %}
<h2>article</h2>
{% endblock %}
</article>
{% include 'label/footer.html' %}
footer.html
<footer>
<h1>footer</h1>
</footer>
</body>
</html>
extens.html 主要注意 extends
表示继承某个页面
{% extends 'label/base.html' %}
{% block title %}
<title>123 page</title>
{% endblock %}
网友评论