模板的基本使用

Bottle内置了一个快速强大的模板引擎,称为SimpleTemplate模板引擎。可通过template()

数或view()修饰器来渲染一个模板。只需提供模板的名字和传递给模板的变量。如下:

[root@jubottle]# tree .

.

├── templ.py

└── views

    └── hello.tpl

1directories, 2files

 

[root@jubottle]# cat templ.py 
#!/usr/bin/envpython
#coding=utf-8
frombottle import route,run,template
 
@route('/hello')
defhello():
    return template('hello')
 
run(host='0.0.0.0',port=8000,debug=True)
[root@jubottle]# cat views/hello.tpl 
<html>
    <head>
    <title>hello page!</title>
    </head>
    <body>
    <h1>hello world!</h1>
    <h2>hello world!</h2>
    <h3>hello world!</h3>
    </body>
</html>

在浏览器中输入:http://192.168.116.199:8000/hello

wKiom1WFLRKRpwqzAACT1Wej_jA448.jpg

默认情况,Bottle会在./views/目录查找模板文件(或当前目录)。也可以使用bottle.TEMPLATE_PATH.append('目录地址')的方式来增加更多的模板路径。

 

给模板传递数据

[root@jubottle]# cat templ.py 
#!/usr/bin/envpython
#coding=utf-8
frombottle import route,run,template
 
@route('/hello')
defhello():
    name="乾楠有"
    age="29"
    weight="138"
    blog="http://changfei.blog.51cto.com"
    returntemplate('hello_template',name=name,age=age,weight=weight,blog=blog)
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>姓名:{{ name }}</p>
    <p>年龄:{{ age }}</p>
    <p>体重:{{ weight}}</p>
    <p>博客:{{ blog }}</p>
    </body>
</html>

在浏览器中输入:http://192.168.116.199:8000/hello

 wKiom1WFLSfireCyAAC01LgD9Qk615.jpg



使用view()修饰器来渲染模版

view()修饰器允许你在回调函数中返回一个字典,并将其传递给模板,和template()函数做同样的事情。

[root@ju bottle]# cat templ.py 
#!/usr/bin/env python
#coding=utf-8
from bottle import route,run,view     #导入view模块
 
@route('/hello')
@view('hello_template') #使用view()修饰器来渲染模版
def hello():
   name="乾楠有"
   age="29"
   weight="138"
   blog="http://changfei.blog.51cto.com"
   info={'name':name,'age':age,'weight':weight,'blog':blog} #这里使用字典传送数据到模版
   return info
run(host='0.0.0.0',port=8000,debug=True)

 

在浏览器中输入:http://192.168.116.199:8000/hello

 wKioL1WFLu3wWlB0AAC01LgD9Qk327.jpg



在模版中显示一个列表或字典传过来的值

[root@ju bottle]#cat templ.py 
#!/usr/bin/envpython
#coding=utf-8
from bottle importroute,run,view     #导入view
 
@route('/hello')
@view('hello_template')#使用view()修饰器来渲染模版
def hello():
    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@ju bottle]#cat views/hello_template.tpl 
<html>
    <head>
    <title>my infomatiom!</title>
    </head>
    <body>
    <h1>My infomation</h1>
    <p>姓名:{{ name}}</p>
    <p>年龄:{{age.get('age') }}</p>              #使用字典的get方法获取值
    <p>体重:{{weight.get('weight')}}</p>
    <p>博客:{{ blog}}</p>
    <p>朋友圈:
    %for i in SNS:             #遍历SNS这个列表
        {{ i }} 
    %end
    </p>
    </body>
</html>

在浏览器中输入:http://192.168.116.199:8000/hello

wKiom1WFLWLyXPgTAAC_Z84bMKI804.jpg