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变量内置过滤器:
capitalize 把值的首字母转换成大写,其他字母转换成小写
lower 把值转换成小写形式
upper 把值转换成大写形式
title 把值中每个单词的首字母都转换成大写
trim 把值的首尾空格去掉
striptags 渲染之前把值中所有的HTML标签都删掉
safe 渲染值时不转义
实例(变量与过滤器)
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('index.html',
message=message,
names = li,
info=info,
fentiao=fentiao,
tags = tags
)
# **********************自定义过滤器******************************
# 定义一个函数
def format_data(s):
return "这是一个过滤器:" + s
# 将该函数添加到默认过滤器中;
app.add_template_filter(format_data, 'fmt')
if __name__ == '__main__':
app.run()
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--外部传入参数-->
<p>{{ names }}</p>
<p>{{ info }}</p>
<p>{{ fentiao }}</p>
<!--内置过滤器-->
<p>{{ message | capitalize }}</p>
<!--把值的首字母转换成大写, 其他字母转换成小写-->
<p>{{ message | lower }}</p>
<!--把值转换成小写形式-->
<p>{{ message | upper }}</p>
<!--把值转换成大写形式-->
<p>{{ message | title }}</p>
<!--把值中每个单词的首字母都转换成大写-->
<p>{{ message | trim }}</p>
<!--把值的首尾空格去掉-->
<p>{{ tags | striptags }}</p>
<!--渲染之前把值中所有的HTML标签都删掉-->
<p>{{ tags | safe}}</p>
<!--渲染值时不转义-->
<!--自定义过滤器-->
<p>{{ message | fmt }}</p>
</body>
</html>
2. for循环:
{% for i in li%}
{% endfor %}
3. if语句
{% if user == 'westos'%}
{% elif user == 'hello' %}
{% else %}
{% endif%}
实例(传输数据、for循环与if语句)
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/users/')
def users():
usersinfo = [('user%s' %(i), "password%s" %(i)) for i in range(100)]
return render_template('users.html',
usersinfo = usersinfo
)
if __name__ == '__main__':
app.run()
users.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--<ul>-->
<!--{% for item in usersinfo %}-->
<!--<li><span style="color:green">用户名:{{ item.0 }} -->
<!--密码:{{ item.1 }}</span></li>-->
<!--{% endfor %}-->
<!--</ul>-->
<table border="1">
<tr>
<td>名称</td>
<td>密码</td>
</tr>
{% for item in usersinfo %}
<tr>
<td>{{ item.0 }}</td>
{% if item.1 == 'password2' %}
<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>
4. 宏的操作====相当于函数
- 如何定义宏?
相当于python里面的定义函数, 后面使用的场景: 分页显示
{% macro render(id) %}
<h1>hello world {{ id }}</h1>
{% endmacro %}
- 如何调用宏?
调用定义好的宏(类似于python中的函数)
{{ render(1) }}
{{ render(2) }}
{{ render(3) }}
5. include包含操作
{% include "inclued.html"%}
include与宏的不同在与: 宏像一个函数一样, 可以传递参数, 改变里面的值;
而include只能显示已经设置好的页面。
实例(宏与include)
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/macro/")
def macro():
return render_template('macro.html')
if __name__ == '__main__':
app.run()
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) }}
{% include "inclued.html"%}
</body>
</html>
include.html
内容随意, 会显示在/macro/页面之中。
6. 模板的继承: 一般网站的导航栏和底部不会变化, 为了避免重复编写导航栏信息
实例
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/extends/')
def extends():
return render_template('use_block.html')
if __name__ == '__main__':
app.run()
- 如何定义模板?
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 %}
{% endblock %}
<div style="width: 100px; height: 200px" >这是底部</div>
</body>
</html>
- 如何继承基模板?
use_block.html
{% extends 'base.html'%}
{% block title %}
继承案例
{% endblock %}
{% block body %}
<span style="color: green">这是最新填的block内容</span>
{% endblock %}