-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.py
121 lines (104 loc) · 4.45 KB
/
filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-09-10
# @copyright by hoojo @2018
# @changelog template filter execution inner func
# ===============================================================================
# 标题:template filter execution inner func
# ===============================================================================
# 使用:使用 filter 关键字,处理标签块内部中的数据
# -------------------------------------------------------------------------------
# 描述:filter 标签块允许处理内部的字符串,进行内部函数的处理
# -------------------------------------------------------------------------------
# 内置过滤器列表:http://jinja.pocoo.org/docs/2.10/templates/#list-of-builtin-filters
# -------------------------------------------------------------------------------
# -------------------------------------------------------------------------------
# filter 标签块的字符串转换演示
# -------------------------------------------------------------------------------
from datetime import datetime
from jinja2 import Environment, select_autoescape, FileSystemLoader
# 从指定位置加载模板的环境
loader = FileSystemLoader('.')
# 更多配置参考:http://jinja.pocoo.org/docs/2.10/api/#high-level-api
env = Environment(loader=loader,
autoescape=select_autoescape(['html', 'xml']),
line_statement_prefix="#",
line_comment_prefix="##",
trim_blocks=True, keep_trailing_newline=True, lstrip_blocks=True)
# 定义自定义过滤器:http://jinja.pocoo.org/docs/dev/api/#custom-filters
# 还可以在代码中使用:environmentfilter(),contextfilter()和 evalcontextfilter()
# -----------------------------------------------------------------
def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
return value.strftime(format)
# 设置自定义过滤器
env.filters['datetimeformat'] = datetimeformat
# 获取模板
template = env.get_template('filter-template.html')
class User():
name = "hoojo"
age = 23
__flag = 0
# 渲染模板
result = template.render(user = User(), users = [ User(), User() ], pub_date = datetime.now())
print(result)
'''
过滤器列表
FILTERS = {
'abs': abs,
'attr': do_attr,
'batch': do_batch,
'capitalize': do_capitalize,
'center': do_center,
'count': len,
'd': do_default,
'default': do_default,
'dictsort': do_dictsort,
'e': escape,
'escape': escape,
'filesizeformat': do_filesizeformat,
'first': do_first,
'float': do_float,
'forceescape': do_forceescape,
'format': do_format,
'groupby': do_groupby,
'indent': do_indent,
'int': do_int,
'join': do_join,
'last': do_last,
'length': len,
'list': do_list,
'lower': do_lower,
'map': do_map,
'min': do_min,
'max': do_max,
'pprint': do_pprint,
'random': do_random,
'reject': do_reject,
'rejectattr': do_rejectattr,
'replace': do_replace,
'reverse': do_reverse,
'round': do_round,
'safe': do_mark_safe,
'select': do_select,
'selectattr': do_selectattr,
'slice': do_slice,
'sort': do_sort,
'string': soft_unicode,
'striptags': do_striptags,
'sum': do_sum,
'title': do_title,
'trim': do_trim,
'truncate': do_truncate,
'unique': do_unique,
'upper': do_upper,
'urlencode': do_urlencode,
'urlize': do_urlize,
'wordcount': do_wordcount,
'wordwrap': do_wordwrap,
'xmlattr': do_xmlattr,
'tojson': do_tojson,
}
'''