<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 17 過濾器基本使用 網址:[http://jinja.pocoo.org/docs/2.10/](http://jinja.pocoo.org/docs/2.10/) [http://jinja.pocoo.org/docs/2.10/templates/\#builtin-filters](http://jinja.pocoo.org/docs/2.10/templates/#builtin-filters) 過濾器是通過管道符號\(\|\)進行使用的,例如:\(\(name\|length\)\),將返回name的長度。過濾器相當于是一個函數,把當前的變量傳入到過濾器中,然后過濾器根據自己的功能,再返回響應的值,之后再將結果渲染到頁面中。 Jinja2中內置了許多過濾器,在這里可以看到所有的過濾器,現對一些常用的過濾器進行講解: * abs\(value\):返回一個數值的絕對值。例如:-1\|abs。 * default\(value,default\_value,boolean=false\):如果當前變量沒有值,則會使用參數中的值來代替。 name\|default\('xiaotuo'\) == 如果name不存在,則會使用xiaotuo來代替。boolean=False默認是在只 有這個變量為undefined的時候才會使用default中的值,如果想使用python的形式判斷是否為false,則可以傳遞Boolean=true。也可以使用or來代替 * escape\(value\)或e:轉義字符,會將等符號轉義成HTML中的符號。例如content\|escape或content\|e * first\(value\):返回一個序列的第一個元素。name\|first * format\(value,\*args,\*\*kwargs\):格式化字符串。 * last\(value\):返回一個序列的最后一個元素。示例:names\|last。 * length\(value\):返回一個序列或者字典的長度。示例:names\|length * join\(value,d='u'\):將一個序列用d這個參數的值拼接成字符串 * safe\(value\):如果開啟了全局轉義,那么safe過濾器會將變量關掉轉義。示例:content\_html\|safe * int\(value\):將值轉換為int類型 * float\(value\):將值轉換為float類型 * lower\(value\):將字符串轉換為小寫 * upper\(value\):將字符串轉換為大寫 * replace\(value,old,new\):替換將old替換為new的字符串 * truncate\(value,length=253,killwords=False\):截取length長度的字符串 * striptags\(value\):刪除字符串中所有的HTML標簽,如果出現多個空格,將替換成一個空格 * trim:截取字符串前面和后面的空白字符 * string\(value\):將變量轉換成字符串 * wordcount\(s\):計算一個長字符串中單詞的個數 ```text from datetime import datetime from flask import Flask, render_template, url_for app = Flask(__name__) app.config["TEMPLATES_AUTO_RELOAD"] = True # @app.route("/") # def hello_world(): # return render_template("index.html",position="-5") @app.route("/") def index(): context = { 'position':-9, 'signature':'<script>alert("hello")</script>', # 'signature': None, 'article':'hello world', 'persons':[1,2,3], 'age':18, # 'create_time':datetime.datetime.now(), 'create_time':datetime(2018,4,27,23,14,0), } signature = None a = signature or '默認值' # return render_template('index.html',position="-9") return render_template('index.html', **context) # 自定義過濾器 @app.template_filter('my_cut') def cut(value): # replace(oldstring,newstring) value = value.replace("hello","") return value @app.template_filter('handle_time') def handle_time(time): """ time 距離現在的時間是多少 如果時間間隔小于1分鐘以內,那么就顯示'剛剛' 如果是大于1分鐘小于等于1小時以內,那么就顯示'xx分鐘前' 如果是大于1小時小于等于24小時內,那么就顯示'xx小時前' 如果大于24小時小于30天以內,那么就顯示'xx天前' 否則就是具體時間:例2017年10月20日 :param time: :return: """ if isinstance(time,datetime): now = datetime.now() # 兩個時間相減,得到描述 timestamp = (now - time).total_seconds() if timestamp < 60: return '剛剛' elif timestamp < 60*60 and timestamp >= 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) ``` ```text index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>miku</title> </head> <body> {# <p>position:{{ position }}</p>#} {# <p>個數:{{ position|wordcount }}</p>#} <p>----------------------------</p> <p>signature:{{ signature|default("Angle","boolean") }}</p> <p>signature:{{ signature|default("Angle",boolean=True) }}</p> <p>{{ signature }}</p> <p>{{ signature or 'angle' }}</p> <p>----------------</p> {# <p>{{ article|my_cut }}</p> #} <p>{{ article }}</p> <p>-----------------</p> <div> {#% autoescape off % #} <!--禁止轉義--> <p>個性簽名:{{ signature|escape}}</p> <!--轉義--> <p>個性簽名:{{ signature|safe}}</p> {#% endautoescape % #} <p>---------------</p> {{ "%s"|format(signature) }} {{ persons|length }} <p>----------</p> {% if age|int == 18 %} <p>成年</p> {% else %} <p>未成年</p> {% endif %} # 去掉HTML元素標簽 <p>{{ signature|striptags }}</p> </div> <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>

                              哎呀哎呀视频在线观看