一.UIMOTHODS:
1.在项目目录创建uimothods.py文件(名称可以任意)
内容:
def test2(self):
return ('hello uimothods')
2.tornado项目文件中导入并注册:
#导入
from utils import uimothods as mt
#注册
settings = {
'ui_modules': mt
}
3.在html中调用uimethod
{{test2()}}
二.UImodules:
1.在项目目录创建uimodules.py文件(名称可以任意)

from tornado.web import UIModule from tornado import escape class test(UIModule): def render(self, *args, **kwargs): return ('<h1>UIMODULES</h1>') #返回html文件,不转义 #return escape.xhtml_escape('<h1>UIMODULES</h1>') #对返回内容进行转义 def embedded_javascript(self):#在html中插入js return 'a.js' def embedded_css(self):#在html中插入css return 'a.css'
2.tornado项目文件中导入并注册:
#导入
from utils import uimodules as md
#注册
settings = {
'ui_modules': md
}
3.在html中调用uimodules
{% module test(123) %}
二者区别:uimothods用函数定义,uimodule用类的方式定义,且可以返回时直接在模板加入js/css等
二。html转意。
1.在uimodule中可以用:
from
tornado import escape
escape.xhtml_escape('<h1>wupeiqi</h1>')
2.在html中可以直接使用raw
{{ raw test}}
#1.全局转义,在Application中添加配置项
autoescape=None,
#2.在文档最开始添加
{% autoescape None %} #实现整个文档转义
#在开启全局和文档不转义的情况下,可以使用 escape() 来开启变量的转义
{{ atag }} {{ escape(atag) }}
#3.也可以使用 {% raw xxx %} 来输出不转义内容
{% raw atag %}
tornado是默认自动开启转义的,大家可以根据需求来选是否转义,但是要知道转义的本意是来防止浏览器意外执行恶意代码的,所以去掉转义的时候需要谨慎选择