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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                在[上一講](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/311.md)的練習中,列位已經曉得,模板中`{{placeholder}}`可以接收來自python文件(.py)中通過`self.render()`傳過來的參數值,這樣模板中就顯示相應的結果。在這里,可以將`{{placeholder}}`理解為占位符,就如同變量一樣啦。 這是一種最基本的模板顯示方式了。但如果僅僅如此,模板的功能有點單調,無法完成比較復雜的數據傳遞。不僅僅是tornado,其它框架如Django等,模板都有比較“高級”的功能。在tornado的模板中,功能還是很不少的,本講介紹模板語法先。 ## 模板中循環的例子 在模板中,也能像在python中一樣,可以使用某些語法,比如常用的if、for、while等語句,使用方法如下: 先看例子 先寫一個python文件(命名為index.py),這個文件中有一個列表`["python", "www.itdiffer.com", "qiwsir@gmail.com"]`,要求是將這個列表通過`self.render()`傳給模板。 然后在模板中,利用for語句,依次顯示得到的列表中的元素。 ~~~ #! /usr/bin/env python #-*- coding:utf-8 -*- import os.path import tornado.httpserver import tornado.ioloop import tornado.web import tornado.options from tornado.options import define, options define("port", default=8000, help="run on the given port", type=int) class IndexHandler(tornado.web.RequestHandler): def get(self): lst = ["python","www.itdiffer.com","qiwsir@gmail.com"] #定義一個list self.render("index.html", info=lst) #將上述定義的list傳給模板 handlers = [(r"/", IndexHandler),] template_path = os.path.join(os.path.dirname(__file__), "temploop") #模板路徑 if __name__ == "__main__": tornado.options.parse_command_line() app = tornado.web.Application(handlers,template_path) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start() ~~~ 模板文件,名稱是index.html,在目錄temploop中。代碼如下: ~~~ <DOCTYPE html> <html> <head> <title>Loop in template</title> </head> <body> <p>There is a list, it is <b>{{info}}</b></p> <p>I will print the elements of this list in order.</p> {% for element in info %} <!-- 循環開始,注意寫法類似python中的for,但是最后沒有冒號 --> <p>{{element}}</p> <!-- 顯示element的內容 --> {% end %} <!-- 結束標志 --> <br> {% for index,element in enumerate(info) %} <p>info[{{index}}] is {{element}} {% end %} </body> </html> ~~~ 運行上面的程序: ~~~ >>> python index.py ~~~ 然后在瀏覽器地址欄中輸入:`http://localhost:8000`,顯示的頁面如下圖: [![](https://box.kancloud.cn/2015-07-07_559b416c6f550.png)](https://github.com/qiwsir/ITArticles/blob/master/Pictures/31201.png) 在上面的例子中,用如下樣式,實現了模板中的for循環,這是在模板中常用到的,當然,從python程序中傳過來的不一定是list類型數據,也可能是其它類型的序列數據。 ~~~ {% for index,element in enumerate(info) %} <p>info[{{index}}] is {{element}} {% end %} ~~~ 特別提醒注意的是,語句要用`{% end %}`來結尾。在循環體中,用`{{ element }}`方式使用序列的元素。 ## 模板中的判斷語句 除了循環之外,在模板中也可以有判斷,在上面代碼的基礎上,改寫一下,直接上代碼,看官想必也能理解了。 index.py的代碼不變,只修改模板index.html的代碼,重點理解模板中的語句寫法。 ~~~ <DOCTYPE html> <html> <head> <title>Loop in template</title> </head> <body> <p>There is a list, it is <b>{{info}}</b></p> <p>I will print the elements of this list in order.</p> {% for element in info %} <p>{{element}}</p> {% end %} <br> {% for index,element in enumerate(info) %} <p>info[{{index}}] is {{element}} {% if element == "python" %} <!-- 增加了一個判斷語句 --> <p> <b>I love this language--{{element}}</b></p> {% end %} {% end %} {% if "qiwsir@gmail.com" in info %} <!-- 還是判斷一下 --> <p><b>A Ha, this the python lesson of LaoQi, It is good! His email is {{info[2]}}</b></p> {% end %} </body> </html> ~~~ 上面的模板運行結果是下圖樣子,看官對比一下,是否能夠理解呢? [![](https://box.kancloud.cn/2015-07-07_559b417338b34.png)](https://github.com/qiwsir/ITArticles/blob/master/Pictures/31202.png) ## 模板中設置變量 廢話不說,直接上例子,因為例子是非常直觀的: ~~~ <DOCTYPE html> <html> <head> <title>Loop in template</title> </head> <body> <p>There is a list, it is <b>{{info}}</b></p> <p>I will print the elements of this list in order.</p> {% for element in info %} <p>{{element}}</p> {% end %} <br> {% for index,element in enumerate(info) %} <p>info[{{index}}] is {{element}} {% if element == "python" %} <p> <b>I love this language--{{element}}</b></p> {% end %} {% end %} {% if "qiwsir@gmail.com" in info %} <p><b>A Ha, this the python lesson of LaoQi, It is good! His email is {{info[2]}}</b></p> {% end %} <h2>Next, I set "python-tornado"(a string) to a variable(var)</h2> {% set var="python-tornado" %} <p>Would you like {{var}}?</p> </body> </html> ~~~ 顯示結果如下: [![](https://box.kancloud.cn/2015-07-07_559b4177e6364.png)](https://github.com/qiwsir/ITArticles/blob/master/Pictures/31203.png) 看官發現了嗎?我用`{% set var="python-tornado" %}`的方式,將一個字符串賦給了變量`var`,在下面的代碼中,就直接引用這個變量了。這樣就是實現了模板中變量的使用。 Tornado的模板真的功能不少呢。不過遠非這些,后面還有。敬請等待。
                  <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>

                              哎呀哎呀视频在线观看