*继承与重写
{% extends "base.html" %}
<!--继承base.html-->
{% block body %}
<!--重写base.html中的block body-->
{% if error %}
<span style="...">Error:{{ error}} </span><p></p>
{% end %}
<form action="/auth/login" method="post">
email:<input name = 'email' type="text"><br>
<p></p>
password:<input name = 'password' type="password"><br>
<p></p>
{% module xsrf_form_html() %}
<!--防止csrf-->
<input type="submit">
</form>
{% end %} this text is not shown
*其他用法
**{%apply *function* %}...{% end %}
以function处理apply 与end 之间的内容
{% apply linkify %} http://www.baidu.com{% end %}
自带linkify 将在页面显示链接
自定义方法
在handler里定义函数, 添加到self.ui字典
class HomeHandler(tornado.web.RequestHandler):
def test_string(self, msg):
msg = msg.decode('utf-8')
return '<a href=%s>%s</a>' % (msg,msg)
def get(self, *args, **kwargs):
self.ui['test_function'] = self.test_string
self.render('login.html', error ='错误')
使用自定义函数
{% apply test_function %}http://www.bing.com{% end %}
**tornado默认转义所有字符, 默认autoescape,
可以用{% raw ...%} 知名局部不转义内容
也可以使用{% autoescape None%}使本文件内容默认不转义
配合escape达到局部转义
** {% for *var* in *expr*%} ... {% end %}
for循环, 也可以使用 {% break %} {% continue %}
** {% if *condition* %} ... {%elif *condition* %} ...{%else%}..{%end%}
** {% include *filename* %}
html元素累加,和base不同 base必须放第一行
** {% set x = ...%} 配合{{x}}使用