tornado中使用Mako模版

tornado中使用Mako模版

tornado是一个优秀的python的开源web 框架,框架本身的性能确实很好,但是他自带的模版只能说是一般般。关于tornado的详细信息可以直接到管网参考。

  http://www.tornadoweb.org/en/stable/

  Mako是python的一个优秀的模版引擎,其性能在模版引擎中排名比较靠前的,关于Mako,Django,Tornado等这些模版的性能比较,可以直接Google一下,不过貌似都是一篇博客被各种转载,当然可以照这他的源代码自己运行一下来判断。Mako的管网。http://www.makotemplates.org/ 对其又详细的介绍。

  sudo pip install Mako 即可安装。

  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()可以获得。

在render_string(self, template_path, **kwargs)中,首先通过get_tempalte(tempalte_path)函数获取模版实例,然后通过get_template_namespace()获取系统定义的一些变量。以字典存储,最后根据用户传递的变量,扩展namespace,直接调用模版的render(**namespace)方法直接输出。

render(self, template_path, **kwargs) 方法比较简单,只调用了self.finish()犯法, finish()方法会调用write(chunk) 响应的。

  这样,我们就完成了Mako和Tornado的集成,下面是一个简单是示例:

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 即可看到结果,如图:

 
 
 
标签: pythontornadoMako
原文地址:https://www.cnblogs.com/Leo_wl/p/3274305.html