tornado之子模板
#!/usr/bin/env python26 #-*- coding:utf8 -*- import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import os.path from tornado.options import define,options define("port",default=8001,help="run on this port",type=int) class TestHandler(tornado.web.RequestHandler): def get(self): self.render('index.html',header_text="Header goes here",footer_text="Footer goes here") def main(): tornado.options.parse_command_line() app=tornado.web.Application( handlers=[(r"/",TestHandler)], template_path=os.path.join(os.path.dirname(__file__),'templates'), debug=True ) http_server=tornado.httpserver.HTTPServer(app) try: http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt,e: print e if __name__ == "__main__": main()
index.html
{% extends main.html %}
{% block header %}
<h1>{{ header_text}} </h1>
{% end %}
{%block body %}
<p> Hello from the child template!</p>
{% end %}
{% block footer %}
<p> {{footer_text}} </p>
{% end %}
main.html
<html>
<body>
<header>
{% block header %}{%end%}
</header>
<content>
{% block body %}{%end%}
</content>
<footer>
{% block footer %}{%end%}
</footer>
</body>
</html>
浙公网安备 33010602011771号