zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    第一次Java作业
    第十一次作业
    第十次作业
    第九次作业
    第八次作业
    第七次作业
    第六次作业
    Java中数组自定义排序与优先级队列笔记
    力扣每日一题:删除链表的倒数第N个节点
    又一次离谱的错误——运算符优先级
  • 原文地址:https://www.cnblogs.com/wbdream/p/10544984.html
Copyright © 2011-2022 走看看