bottle框架学习(三)之模版的使用

模板的基本使用

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

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

[[email protected]]# tree .

.

├── templ.py

└── views

└── hello.tpl

1directories, 2files

[[email protected]]# 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)
[[email protected]]# 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

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

给模板传递数据

[[email protected]]# 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)
[[email protected]]# 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

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

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

[[email protected] 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

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

[[email protected] 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)
[[email protected] 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

时间: 2024-08-05 11:09:47

bottle框架学习(三)之模版的使用的相关文章

Struts2框架学习(三) 数据处理

Struts2框架学习(三) 数据处理 Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理. 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言. 值栈:ValueStack一种数据结构,操作数据的方式为:先进后出 OGNL : Object-GraphNavigation Language(对象图形导航语言)将多个对象的关系使用一种树形的结构展现出来,更像一个图形,那么如果需要对树形结构的节点数据进行操作,那么可以使用 对象.属性 的方式进行操作,OGNL技

bottle框架学习(八)之Mysql数据库的操作

MySQLdb 是用于Python连接Mysql数据库的接口. 安装MySQLdb [[email protected] bottle]# yum install MySQL-python –y 如果使用编译安装请到这里下载安装包https://pypi.python.org/pypi/MySQL-python并执行以下命令 [[email protected] bottle]# gunzip  MySQL-python-1.2.3.tar.gz [[email protected] bottl

bottle框架学习(一)之安装以及路由的使用

安装bottle: [[email protected] bottle]#  yum install python-devel python-setuptools -y [[email protected] bottle]#  easy_install pip [[email protected] bottle]#  pip install bottle 官方文档:http://www.bottlepy.org/docs/dev/index.html 静态路由 [[email protected

bottle框架学习(四)之模版进阶使用

内嵌语句 只要在{{...}} 中的Python 语句返回一个字符串或有一个字符串的表达形式,它就是一个有效的语句. >>>from bottle import template >>>template('hello {{name}}', name='ju') u'helloju' >>>template('hello {{name if name else "world!"}}', name=None) u'helloworld!

Android 学习笔记之AndBase框架学习(三) 使用封装好的函数完成Http请求..

PS:踏踏实实走好每一步... 学习内容: 1.使用AndBase框架实现无参Http Get请求... 2.使用AndBase框架实现有参Http Post请求... 3.使用AndBase框架实现有参Http Get请求...   AndBase框架为我们提供了一些相关的方法提供给我们使用,用来完成Http网络请求...总体就是对Http请求的一个封装,不过个人认为,网络请求这一模块更加推荐使用Volley框架..楼主对比了两个框架中的源码...Volley更多的地方是使用抽象方法封装在接口

bottle框架学习(五)之文件的下载与上传

下载文件 Bottle文件下载还是使用static_file这个模块,只是多加了一个参数:download=True,还是看例子吧! [[email protected]]# cat download.py #!/usr/bin/envpython #coding=utf-8 frombottle import route,run,view,static_file   @route('/download/<filename:path>') defdownload(filename):     

bottle框架学习(六)之错误与重定向

[[email protected] bottle]# cat error.py #!/usr/bin/env python #coding=utf-8 from bottle importroute,run,error,abort,redirect   #访问页面出错,Bottle会显示一个默认的错误页面,提供足够的debug信息.你也可以使用error()函数来自定义你的错误页面 @error(404) def error404(error):    return '访问出错啦!' #一般返

bottle框架学习(七)之session的使用

Bottle 自身并没有提供Session的支持,,可以使用beaker中间件或自己实现.Beaker是一个缓存和会话的库,与Web 应用程序和独立的Python 脚本及应用程序一起使用.它是WSGI 的中间件,能够很简单地嵌入,与基于 WSGI 的 Web 应用程序一起使用,并且缓存修饰器对于任何基于 Python 的应用程序都是容易使用的. beaker文档:http://beaker.readthedocs.org/en/latest/index.html 安装beaker [[email

bottle框架学习(二)之HTTP请求

HTTP 请求方法 HTTP 协议有很多种请求方法.route默认使用GET方法,只响应GET请求.method参数可以给route()函数指定使用哪种方法.或用get(),post(),put()或delete()等函数来代替route()函数. POST 方法一般用于HTML 表单的提交.下面是一个使用POST 来实现用户登录的例子: [[email protected]]# cat login.py #!/usr/bin/envpython #coding=utf-8 from bottl