Tornado-Lesson05-模版继承、函数和类导入、ui_methods和ui_modules

一、模版继承

  1.extends

    {% extends  filename %}继承模版,子模版会继承父模版所有内容,减少大量代码重复。

    注意:一个子模版只能继承一个模版

  2.{% block argname %}...{% end %}  

    block包裹住一段代码,

    argname被包裹的代码块可以在子模版中可以被重写,覆盖父模版。

二、函数和类导入

  1.include

    {% include ./include.html %}  导入模版,模版文件为html文件,不用写头之类多余的,只要写要导入的块就好   

    例如:include.html

<br>this is tornado templates include<br>

三、ui_methods和ui_modules

  1.函数导入和类的导入

    1)新建文件ui_methods.py(文件名随意,只要合法就可以)

'''
this is ui_methods
'''
def methods(self):
    return 'ui_methods'

    新建文件ui_modules.py

'''
this is ui_modules
'''
from tornado.web import UIModule

class UiModule(UIModule):
    def render(self, *args, **kwargs):
        return 'my name is ui_modules'

    2)在项目中导入  

      import util.ui_methods
      import util.ui_modules

    3)配置Application参数    

      ui_methods=util.ui_methods,
      ui_modules=util.ui_modules,
      或者写成字典形式:
      ui_modules={'UiModule':util.ui_modules.UiModule}

    4)在模版中调用   

      传 入 类:{% module UiModule() %}     
      传入函数:{{ methods() }}

四、模版其他引用

  1. apply  

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

    以下结果hello和hahaha都变成大写显示,看结尾例子。

    {# 把end之前的内容放到 upper方法里执行 #}
    {% apply upper %}
    hello<br>
     hahaha{%end%}

  2. linkify

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

    {{ linkify('百度: https://www.baidu.com') }}<br>
    {% raw linkify('百度: https://www.baidu.com') %}

例子:

  父模版mybase.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}Mybase{%end%}</title>
    <link rel="shortcut icon" href="{{static_url('images/favicon.ico')}}">
</head>
<body>
    {% block body %}
    This is Tornato!
    <img src="{{static_url('images/favicon.ico')}}">
    {%end%}
</body>
</html>

  子模版mybase_02.html

{% extends mybase.html %} {#模版继承#}

{% block title %}Child{% end %}

{% block body %}
This is Child!
<img src="{{static_url('images/favicon.ico')}}">

{% include myinclude.html %}    {# 模版导入 #}
{{haha()}}
<br>
{{cla()}}
<br>
{{cla().sum(1,2)}}
<br>
<br>
{# 模版中导入模块 #}
{% import time %}
{{ time.time() }}
<br>
{% from util.mod_file import add, upper %}
{{add(3,4)}}
{{upper('a')}}
<br>
{% module UiModule() %}
<br>
{{ methods() }}
<br>
{# 把end之前的内容放到 upper方法里执行 #}
{% apply upper %}
    hello<br>
    hahaha{%end%}

<br>
{{ linkify('百度: https://www.baidu.com') }}<br>
{% raw linkify('百度: https://www.baidu.com') %}


{% end %}

  要导入的html模块myinclude.html

<br>
    This is include!
<br>

  方法类mod_file.py

def add(a, b):
    return a + b

def upper(a):
    return a.upper()

  方法类ui_methods.py,里面方法作为函数导入 

'''
this is ui_methods
'''
def methods(self):
    return 'ui_methods'

  方法类ui_methods.py,里面方法作为函数导入 

'''
this is ui_modules
'''
from tornado.web import UIModule

class UiModule(UIModule):
    def render(self, *args, **kwargs):
        return 'my name is ui_modules'

  python类extendtest.py

import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.options
import json
import time
import util.ui_methods
import util.ui_modules

from tornado.options import define, options

define('port', default=8080, help='run port', type=int)
define('version', default='0.01', help='version', type=str)

def function_01():
    return 'This is function_01'

class ClassTest:
    def sum(self, a, b):
        return '{a} + {b} = {c}'.format(a = a, b = b, c = a+b)

class LoginHandler(tornado.web.RequestHandler):
    def haha(self):
        return 'this is haha!'

    def get(self, *args, **kwargs):
        self.write('hello word')
        self.render('mybase_02.html',
                    haha = self.haha,
                    cla = ClassTest,
                    )

application = tornado.web.Application(
    handlers = [
    (r"/index", LoginHandler )],
    debug=True,
    static_path = 'static',
    template_path = 'templates',
    ui_methods=util.ui_methods,
    # ui_modules=util.ui_modules,
    ui_modules={'UiModule':util.ui_modules.UiModule}
)

if __name__ == '__main__':
    tornado.options.parse_command_line()
    print(options.port)
    print(options.version)
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

运行结果:

  

原文地址:https://www.cnblogs.com/bear905695019/p/8503606.html