Tornado中的模板语言和django中相似,模板引擎将模板文件载入内存,而后将数据嵌入其中,最终获取到一个完整的字符串,再将字符串返回给请求者。css
Tornado =的模板支持“控制语句”和“表达语句”,控制语句是使用 {%
和 %}
包起来的 例如 {% if len(items) > 2 %}
。表达语句是使用 {{
和 }}
包起来的,例如 {{ items[0] }}
。html
控制语句和对应的 Python 语句的格式基本彻底相同。咱们支持 if
、for
、while
和 try
,这些语句逻辑结束的位置须要用 {% end %}
作标记。还经过 extends
和 block
语句实现了模板继承。这些在 template
模块 的代码文档中有着详细的描述。python
tornado模 板语言,不管for或者if,结尾都是end,不像django的endfor、endif;另外,tornado模板语言,取数据时跟python如出一辙,以下面的取字典里的数据,能够直接dict['key'],也能够dict.get('key','default');不像django里的item.1。
注:在使用模板前须要在setting中设置模板路径:"template_path" : "tpl"jquery
import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.render("index.html", list_info = [11,22,33],title='Mytitle') application = tornado.web.Application([ (r"/index", MainHandler), ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
<html> <head> <title>{{ title }}</title> </head> <body> <ul> {% for item in list_info %} <li>{{item)}}</li> {% end %} </ul> </body> </html>
表达式能够是任意的Python表达式, 包括函数调用. 模板代码会在包含如下对象 和函数的命名空间中执行 (注意这个列表适用于使用 RequestHandler.render
和 render_string
渲染模板的状况. 若是你直接在 RequestHandler
以外使用 tornado.template
模块, 下面这些不少都不存 在)git
在模板中默认提供了一些函数、字段、类以供模板使用: escape: tornado.escape.xhtml_escape 的別名 xhtml_escape: tornado.escape.xhtml_escape 的別名 url_escape: tornado.escape.url_escape 的別名 json_encode: tornado.escape.json_encode 的別名 squeeze: tornado.escape.squeeze 的別名 linkify: tornado.escape.linkify 的別名 datetime: Python 的 datetime 模组 handler: 当前的 RequestHandler 对象 request: handler.request 的別名 current_user: handler.current_user 的別名 locale: handler.locale 的別名 _: handler.locale.translate 的別名 static_url: for handler.static_url 的別名 xsrf_form_html: handler.xsrf_form_html 的別名
<html> <body> <header> {% block header %}{% end %} </header> <content> {% block body %}{% end %} </content> <footer> {% block footer %}{% end %} </footer> </body> </html>
当咱们扩展父模板layout.html时,能够在子模板index.html中引用这些块。github
{% extends "layout.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 %}
<div> <ul> <li>1024</li> <li>42区</li> </ul> </div>
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>title</title> <link href="{{static_url("css/common.css")}}" rel="stylesheet" /> </head> <body> <div class="pg-header"> {% include 'header.html' %} </div> <script src="{{static_url("js/jquery-1.8.2.min.js")}}"></script> </body> </html>
a. 定义web
# uimethods.py def tab(self): return 'UIMethod'
from tornado.web import UIModule from tornado import escape class custom(UIModule): def render(self, *args, **kwargs): return escape.xhtml_escape('<h1>hello world</h1>')
b. 注册数据库
import tornado.ioloop import tornado.web from tornado.escape import linkify import uimodules as md import uimethods as mt class MainHandler(tornado.web.RequestHandler): def get(self): self.render('index.html') settings = { 'template_path': 'template', 'static_path': 'static', 'static_url_prefix': '/static/', 'ui_methods': mt, 'ui_modules': md, } application = tornado.web.Application([ (r"/index", MainHandler), ], **settings) if __name__ == "__main__": application.listen(8009) tornado.ioloop.IOLoop.instance().start()
c. 使用django
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <link href="{{static_url("commons.css")}}" rel="stylesheet" /> </head> <body> <h1>hello</h1> {% module custom(123) %} {{ tab() }} </body>
Tornado默认会自动转义模板中的内容,把标签转换为相应的HTML实体。这样能够防止后端为数据库的网站被恶意脚本攻击。好比,你的网站中有一个评论部分,用户能够在这里添加任何他们想说的文字进行讨论。虽然一些HTML标签在标记和样式冲突时不构成重大威胁(如评论中没有闭<h1>标签),但<script>标签会容许攻击者加载其余的JavaScript文件,打开通向跨站脚本攻击、XSS或漏洞之门。json
全部模板输出默认都会使用 tornado.escape.xhtml_escape
函数转义. 这个行为能够经过传递 autoescape=None
给 Application
或者 tornado.template.Loader
构造器来全局改变, 对于一个模板文件可使 用 {% autoescape None %}
指令, 对于一个单一表达式可使用 {% raw ...%}
来代替 {{ ... }}
. 此外, 在每一个地方一个可选的 转义函数名能够被用来代替 None
.
方法一:是在Application构造函数中传递autoescape=None,另外一种方法是在每页的基础上修改自动转义行为,以下所示:
{% autoescape None %}
{{ mailLink }}
这些autoescape块不须要结束标签,而且能够设置xhtml_escape来开启自动转义(默认行为),或None来关闭。
然而,在理想的状况下,你但愿保持自动转义开启以便继续防御你的网站。所以,你可使用{% raw %}指令来输出不转义的内容。
{% raw mailLink %}