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>
  • 相关阅读:
    过滤非GBK字符
    打印整数数字
    std::string 方法列表
    设计模式——单例模式(Singleton )
    编程之美二进制一的个数
    Jsoncpp试用指南
    GCC下宏扩展后的++i
    关于字节对齐的sizeof的讨论
    Notepad++ 更改和定制主题
    求子数组的最大和
  • 原文地址:https://www.cnblogs.com/wbdream/p/10544984.html
Copyright © 2011-2022 走看看