Tornado Learning (Five)

Tornado comes with a template system , with little Django template syntax differences. Here briefly describes how to use the  Tornado  template system.

The first is to write rules and URL Handler:

Copy the code
01    class NowaMagicHandler(tornado.web.RequestHandler):
02        def get(self):
03            content = u'Welcome to NowaMagic.'
04            #self.write( content )
05            self.render("index.html")
06     
07    def main():
08        tornado.options.parse_command_line()
09        application = tornado.web.Application([
10            (r"/", MainHandler),
11            (r"/nowamagic/", NowaMagicHandler),
12        ],**settings)
13        http_server = tornado.httpserver.HTTPServer(application)
14        http_server.listen(options.port)
15        tornado.ioloop.IOLoop.instance().start()
Copy the code

Then index.html

Copy the code
01    <html>
02    <head>
03    <title>{{ title }}</title>
04    </head>
05    <body>
06      <h1>{{ title }}</h1>
07      <ul>
08        {% for item in items %}
09          <li>{{ escape(item) }}</li>
10        {% end %}
11      </ul>
12    </body>
13    </html>
Copy the code

Is a file containing {% include 'header.html'%} this syntax, and in the same Django.

There is a process for static files, generally called static build a file folder, and then js, css, images classified into them. Of course, you have to write a setting in the program:

Copy the code
1    import os
2     
3    settings = { 
4        "static_path" : os.path.join(os.path.dirname(__file__), "static"), 
5        "template_path" : os.path.join(os.path.dirname(__file__), "templates"), 
6        "gzip" : True, 
7        "debug" : True, 
8    }
Copy the code

setting was also developed a template path. On this setting, more can refer to this article mentioned: how to open Tornado debug mode .

In this way, Tornado templates on OK.

Guess you like

Origin www.cnblogs.com/wangzhilong/p/12549660.html