zoukankan      html  css  js  c++  java
  • 模板继承和UImethod、UImodules

    模板继承与引用

    主文件05-extendes.py文件:

    class MainHandler(RequestHandler):
        def get(self):
            name = self.get_argument('name','')
            self.render('04-extend.html',username = name)
    
    application = tornado.web.Application(
        handlers=[
            (r'/',MainHandler),
        ],
        debug=True,
        template_path = 'templates',
        static_path='static',
        autoescape = None, #全局取消转义
    )

    render返回的文件04-extend.html

    extends

    {% extend *filename* %}继承模板,在子模板中会把父模板的所有内容都继承到子模板中,减少大量重复代码

    block

    {% block *name* %}...{% end %}  被词语句包裹的代码块在子模板中可以被重写,覆盖父模板中的

    {% extends ./03-base.html %}
    {% block title %} Extend {% end %}
    {% block body %}
    {% include ./05-include.html %}
     <h1> hello world!</h1>
    {% end %}
    {% block image %}
    <img src="{{ static_url('images/01.jpg') }}" alt="">
    
    {% end %}

    继承自基类文件 03-base.html

    <head>
        <meta charset="UTF-8">
        <title> {% block title %}Tornado{% end %}</title>
    </head>
    <body>
        {% block body %}
        <h1>taka</h1>
        <h1>gulu</h1>
            {% end %}
        <h1>litao</h1>
        {% block image %} {% end %}
    
    </body>

    include 引用自05-include.html

    {% include *filename*%} include 可以导入一些其他的模板文件,一般使用 include 的时候,模板文件中不使用 block 块

    {% if username !='' %}
        欢迎 {{ username }} 登录
    <img src="static/images/01.jpg" width="200px" alt="">
    <img src="{{ static_url('images/02.webp') }}" width="200px" alt="">
    {% else %}
        亲,请登录
    {% end %}

    函数跟类导入

    1. 渲染时导入

    import tornado.web
    import tornado.ioloop
    import tornado.httpserver
    import tornado.options
    from tornado.web import RequestHandler
    from tornado.options import define,options
    import time
    
    define('port',default=8080,help='run server',type=int)
    
    class HeiHei:
        def sum(self,a,b):
            return a+b
    
    class MainHandler(RequestHandler):
        def haha(self):
            return 'this is haha function'
        def get(self):
            name = self.get_argument('name','')
            self.render('04-extend.html',
                        username = name,
                        haha = self.haha,   #函数导入
                        heihei = HeiHei()   #类的实例导入
                        )
    
    application = tornado.web.Application(
        handlers=[
            (r'/',MainHandler),
        ],
        debug=True,
        template_path = 'templates',
        static_path='static',
    )

    2.模板中直接导入

    2.1 导入内置模块

    2.2 导入自定义模块

    注意:python解释器查找 mod_file 时是根据 py 文件的路径来查找的,不是根据模板的路径来查找

    {% block body %}
    {% include ./05-include.html %}
     <h1> hello world!</h1>
    {{ haha() }}  #渲染时导入
    <br>
    {{ heihei }}   #渲染时导入
    <br>
    {{ heihei.sum(2,3) }}  #渲染时导入
    <br>
    {% import time %}   #模板中直接导入
    {{ time.ctime() }}
    <br>
    {% from util.mod_file import add_num %}  #导入自定义模块
    {{ add_num(1,11) }}
    {% end %}

    刚才讲到了传入函数和类的两种办法,如果一个函数或类需要在很多模板中被导入那之前的两种方式会不会很繁琐呢?

    3.ui_methods跟ui_modules

    第一步

    新建文件ui_methods.py

    新建文件ui_methods.py ,这里的文件名是随意的只要在import时合法即可,这里可以新建一个文件夹,如util,来放置 ui_methods.py 和 ui_modules.py

    def func1(self):  #self必须加
        return 'ui_methods'

    新建文件ui_modules.py

    新建文件ui_modules.py,使用ui_modules需要继承UIModule类,必须重写render方法

    from  tornado.web import UIModule
    
    class TestModule(UIModule):
        def render(self, *args, **kwargs):
            return 'ui_modules'

    第二步

    在项目中导入,05-extends.py中导入

    import util.ui_modules
    import util.ui_methods

    第三步

    配置 Application 参数

    application = tornado.web.Application(
        handlers=[
            (r'/',MainHandler),
        ],
        ui_methods=util.ui_methods,
        ui_modules=util.ui_modules #也可以写成字典形式
    )

    第四步

    在模板中调用,调用modules中的类时候必须在前面添加module。

    <br>
    {{ func1() }}
    <br>
    {% module TestModule() %}

    案例代码演示:

    1.添加 ui_module

    在 ui_modules 中添加如下代码

    from  tornado.web import UIModule
    
    
    class TestModule(UIModule):
        def render(self, *args, **kwargs):
            return 'ui_modules'
    
    class Advertisement(UIModule):
        def render(self, *args, **kwargs):
            return self.render_string('06ad.html') #返回页面
    
        def javascript_files(self):
            return [        #需要返回多个是需要使用列表
                'js/jquery_1_7.js',
                'js/King_Chance_Layer.js',
                'js/King_layer_test.js'
            ]
    
        def css_files(self):
            return 'css/King_Chance_Layer7.css'

    2.导入文件中提到相应的静态文件

    3.模板中使用

    {% module Advertisement() %} 

    运行结果如下:

    4.其他语法

    apply:使用apply语句,使用函数的作用范围到最近的{%end%}为止

    mod_file.py文件中增加

    def upper(a):
        return a.upper() #字符串变大写

    在模板中直接导入使用:

    {% from util.mod_file import add_num,upper %}
    {{ add_num(1,11) }}
    <br>
    {{ upper('hello world') }}
    {% apply upper %}
    fgafg 2w34ert fgaedf gaedfrd wergazwf dfgSDHQWE sdfasdas SDasda
    JJKL hjghhj ghhjkk qwqwqw sasas sdsdsd sdsd sddf kujhn
    {% end %}

    linkify:生成一个链接,但是要注意模板转义

    在模板中

    {#自动识别a标签#}
    {#与全局转义有关#}
    {{ linkify ('百度:http://www.baidu.com') }}
    <br>
    {% raw linkify ('百度:http://www.baidu.com') %}
    <br>
    ('百度:http://www.baidu.com')
    <br>
  • 相关阅读:
    Leetcode840.Magic Squares In Grid矩阵中的幻方
    Leetcode830.Positions of Large Groups较大分组的位置
    Leetcode830.Positions of Large Groups较大分组的位置
    Leetcode821.Shortest Distance to a Character字符的最短距离
    Leetcode821.Shortest Distance to a Character字符的最短距离
    Leetcode824.Goat Latin山羊拉丁文
    Leetcode824.Goat Latin山羊拉丁文
    Leetcode804.Unique Morse Code Words唯一摩尔斯密码词
    Leetcode804.Unique Morse Code Words唯一摩尔斯密码词
    python 中__name__ = '__main__' 的作用
  • 原文地址:https://www.cnblogs.com/taoge188/p/10630967.html
Copyright © 2011-2022 走看看