模板基础
模板基础
-
1 from tornado.web import RequestHandler 2 from tornado.web import Application 3 from tornado.httpserver import HTTPServer 4 from tornado.ioloop import IOLoop 5 from tornado.options import define, options 6 7 define('port', 80, type=int) 8 9 10 11 class IndexHandler(RequestHandler): 12 def get(self): 13 self.render('index.html', name='ivy') 14 15 application = Application( 16 handlers=[ 17 ('/', IndexHandler), 18 ], 19 template_path='template' 20 ) 21 22 23 if __name__ == '__main__': 24 options.parse_command_line() 25 app = HTTPServer(application) 26 app.listen(options.port) 27 IOLoop.current().start()
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 welcome {{name}} 9 </body> 10 </html>
模板传参
- 模板标签基本和django一置
- raw 转译 {% raw html %}
- 单个文件转译在<!DOCTYPE html>下面加上: {% autoescape None %}
- 全局转译在Application里面添加参数, autoescape=None
- 取消转译{{escape(html) }}
- 静态文件的引入:
- 在Application里面配置static_path='静态文件夹名'
- {{ static_url(静态文件夹下的相对路径的位置) }}