#!/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,
}
'''