內嵌語句
只要在``.``.``.`` 中的Python 語句返回一個字符串或有一個字符串的表達形式,它就是一個有效的語句。
>>>from bottle import template >>>template('hello `name`', name='ju') u'helloju' >>>template('hello {{name if name else "world!"}}', name=None) u'helloworld!' >>>template('hello {{name if name else "world!"}}',name="feige") u'hellofeige'
{{}} 中的Python 語句會在渲染的時候被執行,可訪問傳遞給SimpleTemplate.render()方法的所有參數。默認情況下,自動轉義HTML 標籤以防止XSS ***。可在語句前加上”!”來關閉自動轉義。
>>>template('hello {{name if name else "world!"}}',name="<b>feige</b>") u'hello<b>feige</b>' >>>template('hello {{!name if name else "world!"}}',name="<b>feige</b>") u'hello<b>feige</b>'
在模版中嵌入 Pyhton 代碼
以%開頭,表明這一行是Python 代碼。它和真正的Python 代碼唯一的區別,在於你需要顯式地在末尾添加%end語句,表明一個代碼塊結束。這樣你就不必擔心Python代碼中的縮進問題,SimpleTemplate模板引擎的 parser 幫你處理了。不以%開頭的行,被當作普通文本來渲染。只有在行首的%字符纔有意義,可以使用%%來轉義。%%表示以'%'開頭的一行,%%%表示以'%%'開頭的一行
[root@jubottle]# cat templ.py #!/usr/bin/envpython #coding=utf-8 frombottle import route,run,view @route('/hello') @view('hello_template') defhello(): name="乾楠有" blog="http://changfei.blog.51cto.com" myfriend=['奧巴馬','普京','卡梅倫'] myinfodir={'age':29,'weight':138} info={'name':name,'age':myinfodir,'weight':myinfodir,'blog':blog,'SNS':myfriend} return info run(host='0.0.0.0',port=8000,debug=True)
[root@jubottle]# cat views/hello_template.tpl <html> <head> <title>my infomatiom!</title> </head> <body> <h1>My infomation</h1> <p>姓名: %if name: Hi <b>{{ name }}</b> %else: <i>Hello world</i> %end </p> <p>年齡:{{ age.get('age') }}</p> <p>體重:{{ weight.get('weight')}}</p> <p>博客:{{ blog }}</p> <p>朋友圈: %for i in SNS: {{ i }} %end </p> </body> </html>
在瀏覽器中輸入:http://192.168.116.199:8000/hello
使用%if name時,首先需要在後端已經定義過name變量,即在return時,不然會出錯。如果需要return一個沒有定義的變量,使用{{get('name','feige')}}方式,這個語法的意思是如果檢測到一個沒有定義的變量時,就直接定義這個變量並賦值feige。
模板繼承
模版繼承主要使用%include和%rebase兩個語句實現。
使用%include sub_template [kwargs] 語句來包含其他模板。sub_template 參數是模板的文件名或路徑。[kwargs] 部分是以逗號分開的鍵值對,是傳給其他模板的參數。**kwargs 這樣的語法來傳遞一個字典也是允許的。
[root@jubottle]# cat templ.py #!/usr/bin/envpython #coding=utf-8 frombottle import route,run,view @route('/hello') @view('hello') defhello(): name="乾楠有" age=29 weight=138 info={'name':name,'age':age,'weight':weight} return info run(host='0.0.0.0',port=8000,debug=True)
[root@jubottle]# cat views/hello.tpl <html> <head> <title>my infomatiom!</title> </head> <body> <h1>My infomation</h1> <p>姓名:{{ name }}</p> <p>年齡:{{ age }}</p> %include info.tpl </body> </html> [root@jubottle]# cat views/info.tpl <html> <head></head> <body> <p>體重:{{get('weight','136')}}</p> <p>生日:{{get('brithday','0308')}}</p> </body> </html>
在瀏覽器中輸入:http://192.168.116.199:8000/hello
%rebasebase_template [kwargs] 語句會渲染base_template 這個模板,而不是原先的模板。然後base_template 中使用一個空%include 語句來包含原先的模板,並可訪問所有通過kwargs 傳過來的參數。這樣就可以使用模板來封裝另一個模板,或者是模擬某些模板引擎中的繼承機制。
[root@jubottle]# cat templ.py #!/usr/bin/envpython #coding=utf-8 frombottle import route,run,view #導入view @route('/hello') @view('content')#使用view()修飾器來渲染模版 defhello(): name="乾楠有" age=29 weight=138 info={'name':name,'age':age,'weight':weight} return info run(host='0.0.0.0',port=8000,debug=True)
[root@jubottle]# cat views/base.tpl <html> <head> <title>{{ title or 'DefaltTitle'}}</title> </head> <body> <p>{{ Line or 'whatever' }}</p> %include #這裏需要一個空的%include </body> </html> [root@jubottle]# cat views/content.tpl <p>姓名:{{ name }}</p> <p>年齡:{{ age }}</p> <p>體重:{{get('weight','136')}}</p> <p>生日:{{get('brithday','0308')}}</p> %rebasebase title="This is my main page!",Line="The first line" #繼承base模版,並且向base傳遞了title和Line兩個參數。
在瀏覽器中輸入:http://192.168.116.199:8000/hello
查看一下頁面的源碼: