10、模板中对变量进行过滤

常见过滤器

模板文件

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>过滤器</title>
  </head>
  <body>
    <h2>字符串过滤器:</h2>
    <!-- 如果 name 为空,则用 None 替换它 -->
    <p>{{ student.name | default('None',true) }}</p>
    
    <!--将字符串hello转化成Hello,实现首字母大写,得到 Hello-->
    <p>{{'hello'|capitalize}}</p>
    
    <!-- 全部小写 -->
    <p>{{ 'HELLO' | lower }}</p>
    
    <!--将hello中的字母h替换成x, 得到xello -->
    <p>{{'hello'|replace('h','x')}}</p>


    <h2>列表过滤器:</h2>
    <!--对age进行绝对值运算,得到 18-->
    <p>{{ student.age|abs }}</p>
    
    <!--取得列表中的首个元素,得到 1 -->
    <p>{{[1,80,42,44,77]|first}}</p>
    
    <!--取得列表中的最后一个元素,得到 77  -->
    <p>{{[1,80,42,44,77]|last}}</p>
    
    <!--取得列表中的元素中个数,得到 5  -->
    <p>{{[1,80,42,44,77]|count}}</p>
    
    <!--列表中的元素重新排序,默认按照升序进行排序,得到 [1,42,44,77,80]  -->
    <p>{{[1,80,42,44,77]|sort}}</p>
    
    <!--将列表中的元素合并为字符串,得到 1,80,42,44,77,80    -->
    <p>{{[1,80,42,44,77]|join(',')}}</p>


    <h2>数值过滤器:</h2>
    <!--保留小数点后2位,返回结果为18.88-->
    <p>{{18.8888|round(2,'floor')}}</p>
    
    <!--四舍五入取得整数,得到19.0 -->
    <p>{{18.8888|round}}</p>
    
    <!--进行绝对值运算,得到 2 -->
    <p>{{-2|abs}}</p>
  </body>
</html>

主程序文件

#encoding:utf-8
from flask import Flask,render_template#导入Flask以及render_template模块
app = Flask(__name__)#Flask初始化

@app.route('/')#定义路由
def hello_world():#定义视图函数
     student={#定义字典
         "name":"wangjie",
         "age":-18
     }
     return render_template('index.html', student=student)#渲染模板

if __name__ == '__main__':
     app.run()

自定义过滤器

创建模板文件

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Title</title>
		<style>
			/* 分隔线样式 */
			.line {
				display: inline-block;
				height: 1px;
				width: 100%;
				background: #00ccff;
				overflow: hidden;
				vertical-align: middle;
			}
		</style>
	</head>
	<body>
		<meta charset="UTF-8" />
		<ul>
			{% for goods in goods %}<!--对列表进行遍历-->
			<li style="list-style-type: none">
				{{ goods.name }}<span
					class="{{ loop.index | index_class }}"
				></span>
			</li>
			<!--每3条记录输出一条分割线-->
			{% endfor %}
		</ul>
	</body>
</html>

创建主程序

#encoding:utf-8
from flask import Flask,render_template#导入Flask和render_template模块
app = Flask(__name__)#Flask初始化

@app.route('/')#定义路由
 #视图函数
def hello_world():
     goods = [{'name': '怪味少女开衫外套春秋韩版学生bf原宿宽松运动风2018新款秋装上衣'},
              {'name': 'A7seven 复古百搭牛仔外套女秋季2018新款宽松显瘦休闲夹克衫上衣'},
              {'name': '黑色时尚西装外套女春秋中长款2018新款韩版休闲薄款chic西服上衣'},
              {'name': 'HAVE RICE饭馆 颜值超耐打 复古牛仔外套女短款 2018春秋新款上衣'}
              ]#定义列表goods
     return render_template('index.html',**locals())#渲染模板,并向模板传递值

def do_index_class(index):#定义过滤器函数
     if index % 3==0:#每间隔3行输出line
         return 'line'
     else:
         return ''

# 将 do_index_class 函数定义为 index_class 过滤器
app.add_template_filter(do_index_class, 'index_class')#使用自定义过滤器添加css

if __name__ == '__main__':
    app.run()

参考资料

https://weread.qq.com/web/reader/0a932660718ac6bc0a9702e

posted @ 2022-05-30 01:04  tiansz  阅读(21)  评论(0编辑  收藏  举报