码迷,mamicode.com
首页 > 其他好文 > 详细

自定义模板方法(类似django中的simple_tag) | Tornado

时间:2017-12-23 20:22:25      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:htm   imp   cape   默认   gpo   efi   div   self   前缀   

# 自定义模板方法|相当于django中的simple_tag以及filter;
# 1.uimethods.py
    def tag_one(self):
        print(self)
        return ‘tags‘
# 2.uimodules.py
    from tornado.web import UIModule
    from tornado import escape


    class Custom(UIModule):
        def embedded_css(self):
            # 页面添加css样式
            return ‘body{color:blue;}‘
        def css_files(self):
            # 前端引入css文件
            return ‘set.css‘
        def render(self, *args, **kwargs):
            print(args, kwargs)
            return ‘<h1>miaokela</h1>‘
            # return escape.xhtml_escape(‘<h1>miaokela</h1>‘)
# 5.tornado_test.py
    # uimodules除了显示页面内容,还可以在后台自定制JS与CSS;
    import uimethods as mt
    import uimodules as md
    # 8.配置静态文件
    settings = {
        # 静态文件目录
        ‘static_path‘: ‘static‘,
        # url请求时前缀,如果不加,默认是项目根目录下绝对路径;
        ‘static_url_prefix‘: ‘/test/‘,
        # 模板文件路径
        ‘template_path‘: ‘templates‘,
        # 自定义模板方法
        ‘uimethods‘: mt,
        ‘uimodules‘: md,
    }

# 4.login.html
    <h1>登录:{{ tag_one }}{% module Custom() %}</h1>

 

自定义模板方法(类似django中的simple_tag) | Tornado

标签:htm   imp   cape   默认   gpo   efi   div   self   前缀   

原文地址:http://www.cnblogs.com/pymkl/p/8094075.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!