Flask模板_学习笔记

代码尽在个人github上面:https://github.com/521xueweihanhtml

概述:
    在Flask中,Jinja2默认配置以下:
        A.扩展名为.html,.htm,.xml和.xhtml的模板中开启自动转义。
        B.在模板中可使用{% autoescape %}来手动设置是否转义。
        C.Flask在Jinja2环境中加入一些全局函数和辅助对象(下面写的方法)

1.|safe的做用是关闭转义字符
    转义就是对特殊字符进行转义。特殊字符指的是HTML。
    三种方法关闭转义:
        a.用Markup对象封装。
        b.在模板中使用|safe过滤
        c.临时关闭整个系统的自动转义——{% autoescape false%}
        
2.{{ 这里放变量 }}

3.注册过滤器
    两个方法:手动放入jinja_env中,要么使用template_filter()装饰器git

 a:    @app.template_filter('reverse')
        def reverse_filter(s):
            return s[::-1] # 倒序列表

  b:    def reverse_filter(s):
            return s[::-1]
        app.jinja_env.filters['reverse'] = reverse_filter


一旦注册成功,你就能够在模板中像Jinja2的内建过滤其同样使用过滤器了。  
github

{% for x in mylist | reverse %} # 这里就能够按照你注册的过滤器来进行操做
{% endfor%}

 

 

4.环境处理器的做用是把新的变量自动引入模板环境中,返回是一个字典。——用修饰器:@app.context_processor
    传递的能够是一个变量,也能够是一个函数
传递变量: 
app

@app.context_processor
def inject_user():
    return dict(user = g.user)


            
传递函数:   
函数

@app.context_processor
    def utility_processor(): # 处理货币转换的一个方法
        def format_price(amount, currency = u'$'):
            return u'{0:.2f}{1}'.format(amount, currency)
        return dict(format_price = format_price)        

 


上面的例子把format_price函数传递给全部模板,能够在模板中随意调用spa

{{ format_price(0.33) }}
相关文章
相关标签/搜索