Tornado是一個速度非??斓姆亲枞絎eb框架。得利于其非阻塞的方式和對epoll的運用,Tornado每秒可以處理數以千計的連接,這意味著對于實時Web服務來說,Tornado是一個理想的Web框架。下面就來看看Tornado框架配置和模板引擎簡介相關內容吧。下面就來實現下面截圖所示的內容吧。
Tornao中的模板語言和django中類似,模板引擎將模板文件載入內存,然后將數據嵌入其中,最終獲取到一個完整的字符串,再將字符串返回給請求者。
Tornado 的模板支持“控制語句”和“表達語句”,控制語句是使用 {% 和 %} 包起來的 例如 {% if len(items) > 2 %}。表達語句是使用 {{ 和 }} 包起來的,例如 {{ items[0] }}。
控制語句和對應的 Python 語句的格式基本完全相同。我們支持 if、for、while 和 try,這些語句邏輯結束的位置需要用 {% end %} 做標記。還通過 extends 和 block 語句實現了模板繼承。這些在 template 模塊 的代碼文檔中有著詳細的描述。
注:在使用模板前需要在setting中設置模板路徑:"template_path" : "tpl"。
在模板中默認提供了一些函數、字段、類以供模板使用:
escape: tornado.escape.xhtml_escape 的別名
xhtml_escape: tornado.escape.xhtml_escape 的別名
url_escape: tornado.escape.url_escape 的別名
json_encode: tornado.escape.json_encode 的別名
squeeze: tornado.escape.squeeze 的別名
linkify: tornado.escape.linkify 的別名
datetime: Python 的 datetime 模組
handler: 當前的 RequestHandler 對象
request: handler.request 的別名
current_user: handler.current_user 的別名
locale: handler.locale 的別名
_: handler.locale.translate 的別名
static_url: for handler.static_url 的別名
xsrf_form_html: handler.xsrf_form_html 的別名
除了使用框架自帶的函數外,還可以自定義UIMethod以UIModule,使用方法是在文件中引入模塊,并在Tornado的配置文件中設置。具體的示例代碼如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 演示Tornado模板語言 """ import tornado.web import tornado.ioloop import uimethods as mt import uimodules as md title = "歡迎光臨!Hello World!" LIST_INFO = [] class IndexHandler(tornado.web.RequestHandler): """ 首頁處理 """ def data_received(self, chunk): pass def get(self): name = self.get_argument("name", None) LIST_INFO.append(name) if name else print("name為空") self.render("index.html", title=title, list_info=LIST_INFO) settings = { "template_path": "templates", # 模板文件夾路徑 "static_path": "statics", # 靜態文件夾路徑 'ui_methods': mt, 'ui_modules': md, } app = tornado.web.Application([ (r"/index", IndexHandler), ], **settings) if __name__ == '__main__': app.listen(8080) tornado.ioloop.IOLoop.instance().start()
以上為主文件部分,下面為模板文件部分。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>{{ title }}</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="{{static_url("css/style.css")}}" rel="stylesheet"> </head> <body> <header> {% block header %}{% end %} </header> <content> <form action="/index" method="get"> <input type="text" name="name"> <input type="submit" value="提交"> </form> {% block body %}{% end %} </content> <footer> {% block footer %}{% end %} </footer> </body> </html>
{% extends "main.html" %} {% block header %} <h1>{{ title }}</h1> {# 自定義Tornado模塊 #} {% module Custom(123) %} {# 自定義Tornado函數 #} {{ lower_str(title) }} {% end %} {% block body %} {% for index, item in enumerate(list_info) %} {% if index % 2 == 1 %} <li>{{item}}</li> {% else %} <li style="color: red">{{item}}</li> {% end %} {% end %} {% end %} {% block footer %} <p>首頁結束</p> {% end %}
自定義UIMethod以UIModule部分。
#!/usr/local/env python # coding:utf-8 __author__ = "風輕清淡" def lower_str(self, arg: str): """ :param self: :param arg: str :return: """ return arg.lower()
#!/usr/local/env python # coding:utf-8 from tornado.web import UIModule from tornado import escape __author__ = "風輕清淡" class Custom(UIModule): def render(self, *args, **kwargs): return 'UI_Modules'