<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 19 常用過濾器講解 ```text # 過濾器筆記: ### 什么是過濾器,語法是什么: 1. 有時候我們想要在模版中對一些變量進行處理,那么就必須需要類似于Python中的函數一樣,可以將這個值傳到函數中,然后做一些操作。在模版中,過濾器相當于是一個函數,把當前的變量傳入到過濾器中,然后過濾器根據自己的功能,再返回相應的值,之后再將結果渲染到頁面中。 2. 基本語法:`{{ variable|過濾器名字 }}`。使用管道符號`|`進行組合。 ### 常用過濾器: ###### `default`過濾器: 使用方式`{{ value|default('默認值') }}`。如果value這個`key`不存在,那么就會使用`default`過濾器提供的默認值。如果你想使用類似于`python`中判斷一個值是否為False(例如:None、空字符串、空列表、空字典等),那么就必須要傳遞另外一個參數`{{ value|default('默認值',boolean=True) }}`。 可以使用`or`來替代`default('默認值',boolean=True)`。例如:`{{ signature or '此人很懶,沒有留下任何說明' }}`。 ###### 自動轉義過濾器: 1. `safe`過濾器:可以關閉一個字符串的自動轉義。 2. `escape`過濾器:對某一個字符串進行轉義。 3. `autoescape`標簽,可以對他里面的代碼塊關閉或開啟自動轉義。 ```jinja {% autoescape off/on %} ...代碼塊 {% endautoescape %} ``` ### 常用過濾器: 1. first(value):返回一個序列的第一個元素。names|first。 format(value,*arags,**kwargs):格式化字符串。例如以下代碼: ``` {{ "%s" - "%s"|format('Hello?',"Foo!") }} ``` 將輸出:`Helloo? - Foo!` 2. last(value):返回一個序列的最后一個元素。示例:names|last。 3. length(value):返回一個序列或者字典的長度。示例:names|length。 4. join(value,d=u''):將一個序列用d這個參數的值拼接成字符串。 5. safe(value):如果開啟了全局轉義,那么safe過濾器會將變量關掉轉義。示例:content_html|safe。 6. int(value):將值轉換為int類型。 7. float(value):將值轉換為float類型。 8. lower(value):將字符串轉換為小寫。 9. upper(value):將字符串轉換為小寫。 10. replace(value,old,new): 替換將old替換為new的字符串。 11. truncate(value,length=255,killwords=False):截取length長度的字符串。 12. striptags(value):刪除字符串中所有的HTML標簽,如果出現多個空格,將替換成一個空格。 13. trim:截取字符串前面和后面的空白字符。 14. string(value):將變量轉換成字符串。 15. wordcount(s):計算一個長字符串中單詞的個數。 ### 自定義模版過濾器: 過濾器本質上就是一個函數。如果在模版中調用這個過濾器,那么就會將這個變量的值作為第一個參數傳給過濾器這個函數,然后函數的返回值會作為這個過濾器的返回值。需要使用到一個裝飾器:`@app.template_filter('cut')` ```python @app.template_filter('cut') def cut(value): value = value.replace("hello",'') return value ``` ```html <p>{{ article|cut }}</p> ``` from flask import Flask,render_template from datetime import datetime app = Flask(__name__) app.config['TEMPLATES_AUTO_RELOAD'] = True @app.route('/') def index(): context = { 'position': -9, 'signature': '<script>alert("hello")</script>', 'persons':['zhiliao','ketang'], 'age': "18", 'article': 'hello zhiliao world hello', 'create_time': datetime(2017,10,20,16,19,0) } return render_template('index.html',**context) @app.template_filter('cut') def cut(value): value = value.replace("hello",'') return value @app.template_filter('handle_time') def handle_time(time): """ time距離現在的時間間隔 1. 如果時間間隔小于1分鐘以內,那么就顯示“剛剛” 2. 如果是大于1分鐘小于1小時,那么就顯示“xx分鐘前” 3. 如果是大于1小時小于24小時,那么就顯示“xx小時前” 4. 如果是大于24小時小于30天以內,那么就顯示“xx天前” 5. 否則就是顯示具體的時間 2017/10/20 16:15 """ if isinstance(time,datetime): now = datetime.now() timestamp = (now - time).total_seconds() if timestamp < 60: return "剛剛" elif timestamp>=60 and timestamp < 60*60: minutes = timestamp / 60 return "%s分鐘前" % int(minutes) elif timestamp >= 60*60 and timestamp < 60*60*24: hours = timestamp / (60*60) return '%s小時前' % int(hours) elif timestamp >= 60*60*24 and timestamp < 60*60*24*30: days = timestamp / (60*60*24) return "%s天前" % int(days) else: return time.strftime('%Y/%m/%d %H:%M') else: return time if __name__ == '__main__': app.run(debug=True) 自動轉義過濾器: 1.'safe'過濾器,可以關閉一個字符串的自動轉移 2.'escape'過濾器,對某一個字符串進行轉義 3.'autoescape' 標簽,可以對它里面的代碼塊關閉自動轉義 """Jinja2 {% autoescape off/on%} {{xxx|safe}} {{XXX|escape}} {% endautoescape%} """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>MIKU</title> </head> <body> {# <p>位置的絕對值是:{{ position }}</p>#} {# <p>位置的絕對值是:{{ position|wordcount }}</p>#} {# <p>個性簽名:{{ signature|default('Angle','boolean')}}</p>#} {# <p>個性簽名:{{ signature|default('Angle',boolean=True)}}</p>#} {# <p>個性簽名:{{ signature or 'angle'}}</p>#} {# <p>{{ article|my_cut }}</p>#} {# <p>{{ article}}</p>#} {# <p>個性簽名:{{ signature}}</p>#} {# {% autoescape off %}#} {# <p>個性簽名:{{ signature|escape}}</p>#} {# <p>個性簽名:{{ signature|safe}}</p> {# 轉義 #} {# {% endautoescape %}#} {# {{ "%s" | format(signature) }}#} {# {{ persons|length }}#} {# {% if age|int == 18 %}#} {# <p>成年</p>#} {# {% else %}#} {# <p>未成年</p>#} {# {% endif %}#} {# <p>{{ article|replace("hello","") }}</p>#} <p>{{ signature|striptags }}</p> {# <p>發表時間:{{ create_time|handle_time }}</p>#} </body> </html> ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看