文章目录
1. 什么是Jinja2模板引擎?
1). python的Web开发中, 业务逻辑(实质就是视图函数的内容)和页面逻辑(html
文件)分开的, 使得代码的可读性增强, 代码容易理解和维护;
2). 模板渲染: 在html文件中,通过动态赋值 ,
将重新翻译好的html文件(模板引擎生效) 返回给用户的过程。
3). 其他的模板引擎: Mako, Template, Jinja2
2. 语法
1). Jinja2变量显示语法: {{ 变量名 }}、过滤器
完整的过滤器查看位置: http://jinja.pocoo.org/docs/templates/#builtin-filters
Jinja2变量内置过滤器:
safe 渲染值时不转义
capitalize 把值的首字母转换成大写,其他字母转换成小写
lower 把值转换成小写形式
upper 把值转换成大写形式
title 把值中每个单词的首字母都转换成大写
trim 把值的首尾空格去掉
striptags 渲染之前把值中所有的 HTML 标签都删掉
实现变量过滤器:
from flask import Flask, render_template
app = Flask(__name__)
class User(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "<User: %s>" %(self.name)
@app.route('/')
def index():
message = " this is a Message "
li = ['fentiao', 'fensi', 'fendai']
info = {
'name': 'fentiao',
'age':10
}
fentiao = User(name="粉条", age=5)
tags = "<h1>hello world</h1>"
# 可以传入各种类型的变量
return render_template('06_index.html',
message=message,
names = li,
info=info,
fentiao=fentiao,
tags = tags
)
if __name__ == '__main__':
app.run()
编写06_use_block.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--小写-->
<p>{{ message | lower }}</p>
<!--大写-->
<p>{{ message | upper }}</p>
<!--每个单词第一个字母大写-->
<p>{{ message | title }}</p>
<!--首字母大写-->
<p>{{ message | capitalize }}</p>
<!--列表-->
<p>{{ name }}</p>
<!--字典-->
<p>{{ info }}</p>
<!--对象-->
<p>{{ fentiao }}</p>
<!--删掉HTML标签-->
<p>{{ tags | striptags }}</p>
<!--不转译 转义:<h1>hello world</h1> -->
{{ tags | safe }}
</body>
</html>
-如何自定义过滤器?
# 定义一个函数
def format_data(s):
return "这是一个过滤器:" + s
# 将该函数添加到默认过滤器中;
app.add_template_filter(format_data, 'fmt')
if __name__ == '__main__':
app.run()
06_index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--自定义过滤器-->
<p>{{ message | fmt }}</p>
<!--小写-->
<p>{{ message | lower }}</p>
<!--大写-->
<p>{{ message | upper }}</p>
<!--每个单词第一个字母大写-->
<p>{{ message | title }}</p>
<!--首字母大写-->
<p>{{ message | capitalize }}</p>
<!--列表-->
<p>{{ name }}</p>
<!--字典-->
<p>{{ info }}</p>
<!--对象-->
<p>{{ fentiao }}</p>
<!--删掉HTML标签-->
<p>{{ tags | striptags }}</p>
<!--不转译 转义:<h1>hello world</h1> -->
{{ tags | safe }}
</body>
</html>
2). for循环:
{% for i in li%}
{% endfor %}
举例:
@app.route('/users/')
def users():
usersinfo = [('user%s' %(i), "password%s" %(i)) for i in range(100)]
return render_template('06_users.html',
usersinfo = usersinfo
)
if __name__ == '__main__':
app.run()
06_users.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--border边框粗细-->
<table border="1">
<tr>
<td>名称</td>
<td>密码</td>
</tr>
{% for item in userinfo %}
<tr>
<td>{{ item.0 }}</td>
{% if item.1 =='passwords2' %}
<td><span style="color: red">{{ item.1 }}</span></td>
{% else %}
<td><span style="color: green">{{ item.1 }}</span></td>
{% endif %}
</tr>
{% endfor %}
</table>
</body>
</html>
3). if语句
{% if user == 'westos'%}
{% elif user == 'hello' %}
{% else %}
{% endif%}
4). 宏的操作====相当于函数
- 如何定义宏?
<!--相当于python里面的定义函数, 后面使用的场景: 分页显示-->
{% macro render(id) %}
<h1>hello world {{ id }}</h1>
{% endmacro %}
- 如何调用宏?
<!--调用定义好的宏(类似于python中的函数)-->
{{ render(1) }}
{{ render(2) }}
{{ render(3) }}
@app.route("/macro/")
def macro():
return render_template('06_macro.html')
06_macro.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--相当于python里面的定义函数, 后面使用的场景: 分页显示-->
{% macro render(id) %}
<h1>hello world {{ id }}</h1>
{% endmacro %}
<!--调用定义好的宏(类似于python中的函数)-->
{{ render(1) }}
{{ render(2) }}
{{ render(3) }}
{{ render(4) }}
{{ render(5) }}
{{ render(6) }}
5). include包含操作
如何使用: {% include “06_inclued.html”%}
新建一个06_inclued.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--相当于python里面的定义函数, 后面使用的场景: 分页显示-->
{% macro render(id) %}
<h1>hello world {{ id }}</h1>
{% endmacro %}
<!--调用定义好的宏(类似于python中的函数)-->
{{ render(1) }}
{{ render(2) }}
{{ render(3) }}
{{ render(4) }}
{{ render(5) }}
{{ render(6) }}
{% include "06_inclued.html"%}
</body>
</html>
6). 模板的继承: 一般网站的导航栏和底部不会变化, 为了避免重复编写导航栏信息;
- 如何定义模板?
bootstrap中文网里面有许多模板和样式
设计一个导航栏:
新建一个06_base.html作为基模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %} {% endblock %}</title>
</head>
<body>
<div style="width: 100px; height: 200px" > 这是导航栏</div>
{% block body %}
hello
{% endblock %}
<div style="width: 100px; height: 200px" >这是底部</div>
</body>
</html>
- 如何继承基模板?
新建一个06_use_block.html实现继承06_base.html:自定义模块title、body,其余继承base
{% extends '06_base.html'%}
{% block title %}
继承案例
{% endblock %}
{% block body %}
<span style="color: green">这是最新填的block内容</span>
{% endblock %}