模板繼承與引用javascript
主文件05-extendes.py文件:css
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.htmlhtml
extendsjava
{% extend *filename* %}繼承模板,在子模板中會把父模板的全部內容都繼承到子模板中,減小大量重複代碼python
blockjquery
{% block *name* %}...{% end %} 被詞語句包裹的代碼塊在子模板中能夠被重寫,覆蓋父模板中的web
{% 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.htmlapp
<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 塊tornado
{% 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>