video17 jinja2过滤器

过滤器通过管道符号进行使用。如{{ name | length }}将返回name的长度,过滤器相当于是一个函数。

1 def hello_world():
2 intro = '' #这里为空或None时候会显示默认值。
3 return render_template('index.html',intro=intro)
4
5 {{ intro | default('此人很懒,没留下信息。',boolean =True)}}
或者:
{{ intro or '此人很懒,没留下信息。' }}


这里的or和python中一样,都为真则取第一个,都为假就取后一个。

video20 自定义过滤器

以下为一个字符串替换的过滤器。

 1 @app.route('/')
2 def hello_world():
3 intro = 'hello,world!what are you...?'
4
5 return render_template('index.html',intro=intro)
6
7 @app.template_filter('my_cut')
8 def cut(value):
9 value = str(value).replace('hello','Hi')
10 return value
11
12
13 {{ intro | my_cut }}

下面讲一个时间戳的过滤器。

 1 @app.route('/')
2 def hello_world():
3 intro = 'hello,world!what are you...?'
4 create_time = datetime(2019,8,15,12,32,15) #设置任意时间
5 return render_template('index.html',intro=intro,create_time=create_time)
6
7 @app.template_filter('handle_time') #设置过滤器名称
8 def handle_time(time):
9 if isinstance(time,datetime):
10 now = datetime.now()
11 timestamp = (now - time).total_seconds() #换算为秒
12 if timestamp < 60:
13 return '刚刚'
14 elif timestamp >= 60 and timestamp < 60* 60:
15 return "%s分钟前" %str(int(timestamp / 60))
16 elif timestamp >= 60 * 60 and timestamp < 60 * 60 * 24:
17 return "%s小时前" % str(int(timestamp / 60 / 60))
18 elif timestamp > 60 * 60 * 24:
19 return time.strftime('%Y/%m%d %H:%M') #按照‘2019/0815 12:32’格式输出
20 else:
21 return time.strftime('%Y/%m%d %H:%M')
22
23
24 {{ create_time | handle_time }}

video22 条件判断

video23 for循环

video25 宏

这部分请移步:https://www.cnblogs.com/two-peanuts/p/11341367.html

最新文章

  1. Hibernate4 实例
  2. Provisioning Profile文件在哪找?
  3. 使用jmeter进行性能测试-Jmeter教程及技巧汇总 (转)
  4. poj 1696 Space Ant(模拟+叉积)
  5. 单片机C 语言与汇编语言混合编程
  6. Android新增API之AudioEffect中文API与应用实例
  7. 访何红辉:谈谈Android源码中的设计模式
  8. COM模块三---根的形成和注册代理server(Building and Registering a Proxy DLL)
  9. MySQL 服务日志
  10. gulp基础操作实践
  11. 前端json导入excel中
  12. 2019年华南理工校赛(春季赛)--I--炒股(简单思维水题)
  13. popupWindow设置后完美解决返回键响应无效的方案以及popupWindow背景透明方案
  14. linux--磁盘及文件系统管理
  15. oracle删除表垃圾
  16. [Web 前端] superagent-nodejs处理请求的模块
  17. golang学习笔记 ---面向并发的内存模型
  18. 推荐两个Magento做的中文网站 GAP和佰草集
  19. 实习小结(五)--- SSM框架搭建(Maven项目)
  20. laravel 如何引入自己的函数或类库

热门文章

  1. Go strconv包
  2. 感觉学java学到自己的瓶颈期了,各种框架乱七八糟,感觉好乱。该怎么办!?
  3. 强大的table组件-antd pro table
  4. 在shell中截取心仪的字符串
  5. 数据库SQL Server 2016“功能选择”详细说明及精简安装选择
  6. Vue.js 学习笔记之五:编译 vue 组件
  7. centos8平台使用lsof
  8. java 实体对象转Map公共类
  9. 【服务总线 Azure Service Bus】ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题
  10. tomcat在eclipse里部署