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 UIModuleclass UiModule(UIModule):def render(self, *args, **kwargs):return 'my name is ui_modules'2)在項目中導入
import util.ui_methodsimport 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 + bdef 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 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()運行結果:
?
轉載于:https://www.cnblogs.com/bear905695019/p/8503606.html
總結
以上是生活随笔為你收集整理的Tornado-Lesson05-模版继承、函数和类导入、ui_methods和ui_modules的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [patl2-001]紧急救援
- 下一篇: 【XSY2667】摧毁图状树 贪心 堆