tornado一起来-模版继承
模版继承,这么高大上的东西怎么能没有?!
主要内容参考[《Introduction to Tornado》中文翻译](http://demo.pythoner.com/itt2zh)
走起!
来个main.py
```
import os.path<br></br>import random<br></br><br></br>import tornado.httpserver<br></br>import tornado.ioloop<br></br>import tornado.options<br></br>import tornado.web<br></br><br></br>from tornado.options import define, options<br></br>define("port", default=8000, help="run on the given port", type=int)<br></br><br></br>class IndexHandler(tornado.web.RequestHandler):<br></br> def get(self):<br></br> self.render(<br></br> "index.html",<br></br> header_text="Header goes here",<br></br> footer_text="Footer goes here"<br></br> )<br></br><br></br>if __name__ == '__main__':<br></br> tornado.options.parse_command_line()<br></br> app = tornado.web.Application(<br></br> handlers=[(r'/', IndexHandler)],<br></br> template_path=os.path.join(os.path.dirname(__file__), "templates"),<br></br> static_path=os.path.join(os.path.dirname(__file__), "static"),<br></br> debug=True<br></br> )<br></br> http_server = tornado.httpserver.HTTPServer(app)<br></br> http_server.listen(options.port)<br></br> tornado.ioloop.IOLoop.instance().start()<br></br>
```
再来个main.html
```
<br></br><br></br> <header><br></br><span> {% block header %}{% end %}<br></br> </header><br></br></span> <content><br></br></content> {% block body %}{% end %}<br></br> <br></br> <footer><br></br><span> {% block footer %}{% end %}<br></br> </footer><br></br></span><br></br>
```
子html index.html
```
{% extends "main.html" %}<br></br><br></br>{% block header %}<br></br> <h1>{{ header_text }}<span></h1><br></br></span>{% end %}<br></br><br></br>{% block body %}<br></br> <p>Hello from the child template!<span></p><br></br></span>{% end %}<br></br><br></br>{% block footer %}<br></br> <p>{{ footer_text }}<span></p><br></br></span>{% end %}
```

呵呵 出来了!
(心里默默说,不还是输出个文字么)
好吧 难度稍微提高那么一点点!
main.py
```
import os.path<br></br>import random<br></br><br></br>import tornado.httpserver<br></br>import tornado.ioloop<br></br>import tornado.options<br></br>import tornado.web<br></br><br></br>from tornado.options import define, options<br></br>define("port", default=8000, help="run on the given port", type=int)<br></br><br></br>class RecommendedHandler(tornado.web.RequestHandler):<br></br> def get(self):<br></br> self.render(<br></br> "index.html",<br></br> page_title="Burt's Books | Recommended Reading",<br></br> header_text="Recommended Reading",<br></br> books=[<br></br> {<br></br> "title":"Programming Collective Intelligence",<br></br> "subtitle": "Building Smart Web 2.0 Applications",<br></br> "image":"/static/images/cover.jpg",<br></br> "author": "Toby Segaran",<br></br> "date_added":1310248056,<br></br> "date_released": "August 2007",<br></br> "isbn":"978-0-596-52932-1",<br></br> "description":"<p>This fascinating book demonstrates how you "<br></br> "can build web applications to mine the enormous amount of data created by people "<br></br> "on the Internet. With the sophisticated algorithms in this book, you can write "<br></br> "smart programs to access interesting datasets from other web sites, collect data "<br></br> "from users of your own applications, and analyze and understand the data once "<br></br> "you've found it.</p>"<br></br> },{<br></br> "title":"Programming Collective Intelligence",<br></br> "subtitle": "Building Smart Web 2.0 Applications",<br></br> "image":"/static/images/cover.jpg",<br></br> "author": "Toby Segaran",<br></br> "date_added":1310248056,<br></br> "date_released": "August 2007",<br></br> "isbn":"978-0-596-52932-1",<br></br> "description":"<p>This fascinating book demonstrates how you "<br></br> "can build web applications to mine the enormous amount of data created by people "<br></br> "on the Internet. With the sophisticated algorithms in this book, you can write "<br></br> "smart programs to access interesting datasets from other web sites, collect data "<br></br> "from users of your own applications, and analyze and understand the data once "<br></br> "you've found it.</p>"<br></br> }<br></br> ]<br></br> )<br></br><br></br>class BookModule(tornado.web.UIModule):<br></br> def render(self, book):<br></br> return self.render_string('modules/book.html', book=book)<br></br><br></br>if __name__ == '__main__':<br></br> tornado.options.parse_command_line()<br></br> app = tornado.web.Application(<br></br> handlers=[(r'/', RecommendedHandler)],<br></br> template_path=os.path.join(os.path.dirname(__file__), "templates"),<br></br> static_path=os.path.join(os.path.dirname(__file__), "static"),<br></br> ui_modules={'Book': BookModule},<br></br> debug=True<br></br> )<br></br> http_server = tornado.httpserver.HTTPServer(app)<br></br> http_server.listen(options.port)<br></br> tornado.ioloop.IOLoop.instance().start()
```
main.html
```
<html><br></br><body><br></br> <header><br></br> <link rel="stylesheet" href="{{ static_url("style.css") }}"><br></br> {% block header %}{% end %}<br></br> </header><br></br> <content><br></br> {% block body %}{% end %}<br></br> </content><br></br> <footer><br></br> {% block footer %}{% end %}<br></br> </footer><br></br></body><br></br></html>
```
index.html
```
{% extends "main.html" %}<br></br><br></br>{% block body %}<br></br><h2>Recommended Reading</h2><br></br> {% for book in books %}<br></br> {% module Book(book) %}<br></br> {% end %}<br></br>{% end %}
```
book.html
```
<div class="book"><br></br> <h3 class="book_title">{{ book["title"] }}</h3><br></br> {% if book["subtitle"] != "" %}<br></br> <h4 class="book_subtitle">{{ book["subtitle"] }}</h4><br></br> {% end %}<br></br> <img src="{{ book["image"] }}" class="book_image"/><br></br> <div class="book_details"><br></br> <div class="book_date_released">Released: {{ book["date_released"]}}</div><br></br> <div class="book_date_added"><br></br> Added: {{ locale.format_date(book["date_added"], relative=False) }}<br></br> </div><br></br> <h5>Description:</h5><br></br> <div class="book_body">{% raw book["description"] %}</div><br></br> </div><br></br></div>
```
style.css
```
body {<br></br> font-family: Helvetica,Arial,sans-serif;<br></br> width: 600px;<br></br> margin: 0 auto;<br></br>}<br></br>.replaced:hover { color: #00f; }<br></br>.book{<br></br> background: #d2cdd1;<br></br> border: 1px solid #5e5d5f;<br></br> padding: 20px;<br></br> margin-bottom: 20px;<br></br> overflow: hidden;<br></br>}<br></br>.book_image{<br></br> max-width: 200px;<br></br> float: left;<br></br>}<br></br>.book_details {<br></br> float: left;<br></br> width: 300px;<br></br> margin-left: 10px;<br></br>}<br></br>
```
最终效果

文件结构是

评论区