内嵌语句
只要在``.``.``.`` 中的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
查看一下页面的源码:
转载于:https://blog.51cto.com/changfei/1663908