日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/4/17 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tornado-Lesson05-模版继承、函数和类导入、ui_methods和ui_modules 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、模版繼承

  1.extends

    {% extends ?filename %}繼承模版,子模版會繼承父模版所有內(nèi)容,減少大量代碼重復(fù)。

    注意:一個子模版只能繼承一個模版

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

    block包裹住一段代碼,

    argname被包裹的代碼塊可以在子模版中可以被重寫,覆蓋父模版。

二、函數(shù)和類導(dǎo)入

  1.include

    {% include ./include.html %}  導(dǎo)入模版,模版文件為html文件,不用寫頭之類多余的,只要寫要導(dǎo)入的塊就好   

    例如:include.html

<br>this is tornado templates include<br>

?

?

三、ui_methods和ui_modules

  1.函數(shù)導(dǎo)入和類的導(dǎo)入

    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 UIModuleclass UiModule(UIModule):def render(self, *args, **kwargs):return 'my name is ui_modules'

    2)在項目中導(dǎo)入  

      import util.ui_methods
      import util.ui_modules

?

    3)配置Application參數(shù)    

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

    4)在模版中調(diào)用   

      傳 入 類:{% module UiModule() %}            傳入函數(shù):{{ methods() }}

?

四、模版其他引用

  1. apply  

    使用apply語句,使函數(shù)的作用范圍到最近的{% end %}為止

    以下結(jié)果hello和hahaha都變成大寫顯示,看結(jié)尾例子。

    {# 把end之前的內(nèi)容放到 upper方法里執(zhí)行 #}
    {% apply upper %}
    hello<br>
     hahaha{%end%}

?

?

  2. linkify

    linkify生成一個鏈接,但是要注意模版轉(zhuǎn)義

    {{ 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 %} {# 模版導(dǎo)入 #} {{haha()}} <br> {{cla()}} <br> {{cla().sum(1,2)}} <br> <br> {# 模版中導(dǎo)入模塊 #} {% import time %} {{ time.time() }} <br> {% from util.mod_file import add, upper %} {{add(3,4)}} {{upper('a')}} <br> {% module UiModule() %} <br> {{ methods() }} <br> {# 把end之前的內(nèi)容放到 upper方法里執(zhí)行 #} {% apply upper %}hello<br>hahaha{%end%}<br> {{ linkify('百度: https://www.baidu.com') }}<br> {% raw linkify('百度: https://www.baidu.com') %}{% end %}

  要導(dǎo)入的html模塊myinclude.html

<br>This is include! <br>

  方法類mod_file.py

def add(a, b):return a + bdef upper(a):return a.upper()

  方法類ui_methods.py,里面方法作為函數(shù)導(dǎo)入?

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

  方法類ui_methods.py,里面方法作為函數(shù)導(dǎo)入?

''' this is ui_modules ''' from tornado.web import UIModuleclass 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_modulesfrom tornado.options import define, optionsdefine('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()

運行結(jié)果:

  

?

轉(zhuǎn)載于:https://www.cnblogs.com/bear905695019/p/8503606.html

總結(jié)

以上是生活随笔為你收集整理的Tornado-Lesson05-模版继承、函数和类导入、ui_methods和ui_modules的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。