Flask模板引擎Jinja2使用实例
作者:gdjlc 时间:2022-01-19 11:58:39
Flask提供的模板引擎为Jinja2,易于使用,功能强大。
模板仅仅是文本文件,它可以生成任何基于文本的格式(HTML、XML、CSV、LaTex 等等)。
它并没有特定的扩展名, .html 或 .xml 都是可以的。
模板包含 变量 或 表达式 ,这两者在模板求值的时候会被替换为值。模板中还有标签,控制模板的逻辑。
Jinja2文档:http://docs.jinkan.org/docs/jinja2/index.html
下面是一些使用实例,涉及模板继续、变量、赋值、循环、去空白、转义块、条件语句等。
1、在目录templates下面创建基本模板文件base.html
<!doctype html>
<html>
<head>
<title>{{title}}</title>
<style text="text/css">
ul{list-style:none}
li{padding-left:20px}
</style>
</head>
<body>
<div id="content">
<h4>子模板内容</h4>
{% block content %}
{% endblock %}
</div>
</body>
</html>
2、同样在目录templates下面创建子模板文件test1.html
{% extends 'base.html' %}
{# 这里是注释,上面标签表示当前模板继承自模板base.html #}
{% block content %}
变量:{{v1}}
<br />
赋值:
{% set v2 = 100 %}
{{v2}}
<br />
循环:
{% for item in items%}
{{ item.name }}
{% endfor %}
<br />
去空白:
{% for item in items -%}
{{ item.name }}
{%- endfor %}
<br />
转义块:
{% raw %}
<ul>
{% for item in list -%}
<li>{{ item }}</li>
{%- endfor %}
</ul>
{% endraw %}
条件语句:
{% for item in items%}
{% if item.name == '张三' %} {{item.name}}(管理员)
{% else %} {{item.name}}
{% endif %}
{% endfor %}
<br />
<form method="post" action="/getFormValue"> <input type="text" name="username" width="60" /> <input type="submit" value="获取表单值" /> </form>
{% endblock %}
3、在与目录templates平级目录上创建test1.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/test1')
def test1() -> 'html':
items = [{'name':'张三'},
{'name':'李四'},
{'name':'王五'}]
return render_template('test1.html',
title='test',
v1='变量值',
items = items)
@app.route('/getFormValue', methods=['POST'])
def getFormValue() -> 'str':
username = request.form['username']
return username
app.run(debug = True)
4、在命令提示符下执行py -3 test1.py
访问http://127.0.0.1:5000/test1,页面显示如下:
子模板内容
变量:变量值
赋值: 100
循环: 张三 李四 王五
去空白: 张三李四王五
转义块:
{% for item in list -%}
{{ item }}
{%- endfor %}
条件语句: 张三(管理员) 李四 王五
来源:https://www.cnblogs.com/gdjlc/p/11072835.html
标签:Flask,模板,引擎,Jinja2
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python字节码与程序执行过程详解
2022-01-25 04:45:24
![](https://img.aspxhome.com/file/2023/0/64600_0s.png)
Pandas.DataFrame转置的实现 <font color=red>原创</font>
2022-03-02 03:07:34
PHP如何利用P3P实现跨域
2023-11-23 20:16:58
关于Python的Thread线程模块详解
2022-12-20 02:48:16
![](https://img.aspxhome.com/file/2023/5/118355_0s.png)
基于Python实现批量保存视频到本地
2023-10-14 12:24:27
![](https://img.aspxhome.com/file/2023/7/118787_0s.png)
简述Asp与XML之间的关系
2008-04-17 10:46:00
python自动发邮件库yagmail的示例代码
2022-01-15 14:23:26
![](https://img.aspxhome.com/file/2023/0/83720_0s.png)
符合w3c标准flash插入代码,常用flash参数设置
2009-01-20 18:47:00
keras实现theano和tensorflow训练的模型相互转换
2023-04-18 05:49:26
![](https://img.aspxhome.com/file/2023/1/89081_0s.jpg)
Python3基础之输入和输出实例分析
2022-08-05 11:08:09
python将字符串以utf-8格式保存在txt文件中的方法
2023-01-10 14:04:55
![](https://img.aspxhome.com/file/2023/7/98327_0s.jpg)
利用Python内置库实现创建命令行应用程序
2022-04-26 03:39:19
python中bottle使用实例代码
2023-07-03 14:21:15
![](https://img.aspxhome.com/file/2023/9/105809_0s.png)
JS动态添加与删除select中的Option对象(示例代码)
2023-09-17 02:15:31
使用django和vue进行数据交互的方法步骤
2021-12-20 03:29:07
Python方差特征过滤的实例分析
2021-08-11 01:12:56
python 类相关概念理解
2023-02-17 21:16:47
Python程序中的观察者模式结构编写示例
2022-08-04 22:12:11
![](https://img.aspxhome.com/file/2023/3/66823_0s.png)
Python使用Beautiful Soup实现解析网页
2022-11-21 19:17:22
Python实现程序判断季节的代码示例
2022-04-04 13:30:22
![](https://img.aspxhome.com/file/2023/1/76941_0s.png)