tornado是一個優秀的python的開源web 框架,框架自己的性能確實很好,可是他自帶的模版只能說是通常般。關於tornado的詳細信息能夠直接到管網參考。html
http://www.tornadoweb.org/en/stable/python
Mako是python的一個優秀的模版引擎,其性能在模版引擎中排名比較靠前的,關於Mako,Django,Tornado等這些模版的性能比較,能夠直接Google一下,不過貌似都是一篇博客被各類轉載,固然能夠照這他的源代碼本身運行一下來判斷。Mako的管網。http://www.makotemplates.org/ 對其又詳細的介紹。web
sudo pip install Mako 便可安裝。app
Tornado更換模版引擎比較簡單,只須要重寫 def render_string(self, template_path, **kwargs) 和def render(self, template_path, **kwargs)框架
完整代碼以下:函數
1 #-*- coding: utf-8 -*- 2 import tornado.web 3 import os.path 4 import tornado.ioloop 5 import mako.lookup 6 import tornado.httpserver 7 import mako.template 8 9 class BaseHandler(tornado.web.RequestHandler): 10 11 12 def initialize(self): 13 template_path = self.get_template_path() 14 self.lookup = mako.lookup.TemplateLookup(directories=[template_path], input_encoding='utf-8', output_encoding='utf-8') 15 #self.lookup = mako.lookup.TemplateLookup(directories=[template_path]) 16 17 def render_string(self, template_path, **kwargs): 18 template = self.lookup.get_template(template_path) 19 namespace = self.get_template_namespace() 20 namespace.update(kwargs) 21 return template.render(**namespace) 22 23 def render(self, template_path, **kwargs): 24 self.finish(self.render_string(template_path, **kwargs))
在 def initialize(self)中 定義了模版的搜索路徑,其中模版的搜索路徑只在Application的settings中設定的,self.get_template_path()能夠得到。tornado
在render_string(self, template_path, **kwargs)中,首先經過get_tempalte(tempalte_path)函數獲取模版實例,而後經過get_template_namespace()獲取系統定義的一些變量。以字典存儲,最後根據用戶傳遞的變量,擴展namespace,直接調用模版的render(**namespace)方法直接輸出。oop
render(self, template_path, **kwargs) 方法比較簡單,只調用了self.finish()犯法, finish()方法會調用write(chunk) 響應的。性能
這樣,咱們就完成了Mako和Tornado的集成,下面是一個簡單是示例:spa
main.py
1 #-*- coding: utf-8 -*- 2 import tornado.web 3 import os.path 4 import tornado.ioloop 5 import mako.lookup 6 import tornado.httpserver 7 import mako.template 8 9 10 class BaseHandler(tornado.web.RequestHandler): 11 12 13 def initialize(self): 14 template_path = self.get_template_path() 15 self.lookup = mako.lookup.TemplateLookup(directories=[template_path], input_encoding='utf-8', output_encoding='utf-8') 16 17 def render_string(self, filename, **kwargs): 18 template = self.lookup.get_template(filename) 19 namespace = self.get_template_namespace() 20 namespace.update(kwargs) 21 return template.render(**namespace) 22 23 def render(self, filename, **kwargs): 24 self.finish(self.render_string(filename, **kwargs)) 25 26 27 class IndexHandler(BaseHandler): 28 def get(self): 29 self.render('index.html',title='Text',body='This is body') 30 31 class Application(tornado.web.Application): 32 def __init__(self): 33 handlers = [ 34 (r'/',IndexHandler), 35 ] 36 settings = { 37 'template_path' : os.path.join(os.path.dirname(__file__),'templates') 38 } 39 tornado.web.Application.__init__(self, handlers,**settings) 40 41 if __name__ == '__main__': 42 application = Application() 43 application.listen(8080) 44 tornado.ioloop.IOLoop.instance().start()
將index.html文件放到templates文件夾下
1 ##-*- coding: utf-8 -*- 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title>${title}</title> 7 </head> 8 <body> 9 ${body} 10 </body> 11 </html>
運行 python main.py
輸入網址http://localhost:8080 便可看到結果,如圖: