码迷,mamicode.com
首页 > 其他好文 > 详细

tornada模板学习笔记

时间:2014-11-01 23:12:12      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   使用   sp   

import tornado.web
import tornado.httpserver
import tornado.ioloop
import tornado.options
import os.path

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)

class HelloHandler(tornado.web.RequestHandler):
    def get(self):
        self.render(hello.html)

class HelloModule(tornado.web.UIModule):
    def render(self):
        return <h1>Hello, world!</h1>

if __name__ == __main__:
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers=[(r/, HelloHandler)],
        template_path=os.path.join(os.path.dirname(__file__), templates),
        ui_modules={Hello: HelloModule}
    )
    server = tornado.httpserver.HTTPServer(app)
    server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

ui_moudles参数期望一个模块名为键、类为值的字典输入来渲染它们

这个例子中ui_module字典里只有一项,它把到名为Hello的模块的引用和我们定义的HelloModule类结合了起来。

现在,当调用HelloHandler并渲染hello.html时,我们可以使用{% module Hello() %}模板标签来包含HelloModule类中render方法返回的字符串。

(Hello()相当于调用了HelloModule,因为前面的ui_modules={‘Hello‘: HelloModule} 已经将两者结合,可以看成是一种别名

<html>
    <head><title>UI Module Example</title></head>
    <body>
        {% module Hello() %}
    </body>
</html>

 

tornada模板学习笔记

标签:style   blog   http   io   color   os   ar   使用   sp   

原文地址:http://www.cnblogs.com/linjj/p/4067886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!