tornado 模板引擎

 在tornado的模板引擎中,有两种方式,UImethod与UImodule 自定义方法

在模板中调用方法:

  tornado:与Django一样使用{{}},但是对于for循环之类,Django以{% endfor%}结尾,而tornado以{% end%}结尾。调用字典或者列表,tornado使用list[0],而Django以list.0使用方法不一样。

  要想使用UImethod与UImodule,必须先早app里面的settings中注册:

  

import uimethod as ud
import uimodule as um
settings = {"template_path": 'views',
'cookie_secret': 'ahsfkasjasfasd',
'ui_methods': ud,
'ui_modules': um,
}

然后再在模板中使用uimodule.py

from tornado.web import UIModule
from tornado import escape

class Test(UIModule):

    def render(self, *args, **kwargs):

        return '<h1>ui_module</h1>'  # 不转义
     # return escape.xhtml_escape('<h1>ui_module</h1>') 转义

uimethod.py def tag(request): return '<h1>ui_method</h1>'

模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    uimethod 自动转义:{{tag()}}
    uimethod 不转义:{% raw tag()%}
    uimodule:{% module Test()%}
    欢迎光临
</body>
</html>
原文地址:https://www.cnblogs.com/wbdream/p/10544984.html