内嵌语句
只要在{{…}} 中的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 代码唯一的区别,在于你需要显式地在末尾添加%结束语句,表明一个代码块结束。这样你就不必担心的Python 代码中的缩进问题,SimpleTemplate 模板引擎的解析器帮你处理了。不以%开头的行,被当作普通文本来渲染。只有在行首的%字符才有意义,可以使用%% 来转义。%% 表示以’%’ 开头的一行,%%% 表示以’%%’ 开头的一行
[root@jubottle]# cat templ.py
#!/usr/bin/envpython
#coding=utf-8
frombottle import route,run,view
@route('/hello')
@view('hello_template')
defhello():
name="昝道广"
blog="xzan.ngrok.cc"
myfriend=['小昝','小道','小广']
myinfodir={'age':20,'weight':130}
info={'name':name,'age':myinfodir,'weight':myinfodir,'blog':blog,'SNS':myfriend}
return info
run(host='127.0.0.1',port=80,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 : //127.0.0.1/ hello(这里用的我自己的域名,道理是一样的)
如果需要返回一个没有定义的变量,使用{{get(’name’,’feige’)}} 如果需要返回一个没有定义的变量,则使用%if name 时,首先需要在后端已经定义过名变量,即在返回时,不然会出错。方式,这个语法的意思是如果检测到一个没有定义的变量时,就直接定义这个变量并赋值feige 。
模板继承
模版继承主要使用%包括和%重订两个语句实现。
使用%包括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=20
weight=130
info={'name':name,'age':age,'weight':weight}
return info
run(host='127.0.0.1',port=80,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 : //127.0.0.1/ hello
结果和上边类似
%rebasebase_template [kwargs] 语句会渲染base_template 这个模板,而不是原先的模板。然后base_template 中使用一个空%包括语句来包含原先的模板,并可访问所有通过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='192.168.116.199',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
此时可以观察到结果
注:所博客所有的IP都需要自己配置或者只用用127.0.0.1:80端口
转载自:
http://changfei.blog.51cto.com/4848258/1663908