Last active
March 11, 2016 01:34
-
-
Save zii/eb5a4182d8098fd9f803 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
"""添加自定义过滤器""" | |
from jinja2 import Environment | |
from jinja2.runtime import Undefined | |
def do_date(date, format): | |
"""时间格式化 | |
~ 用法:{{xxx|date('%Y-%m-%d %H:%M:%S')}} | |
""" | |
s = date.strftime(format.encode('utf-8')) | |
return s.decode('utf-8') | |
filters = { | |
'date': do_date, | |
} | |
def finalize(o): | |
"把None换成空" | |
return '' if o is None else o | |
class Env(Environment): | |
def __init__(self, *args, **kw): | |
Environment.__init__(self, *args, **kw) | |
# 把未定义变量和None处理为空字符串 | |
self.finalize = finalize | |
self.undefined = Undefined | |
self.filters.update(filters) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment