设为首页 收藏本站
查看: 926|回复: 0

[经验分享] python的web框架webpy【Templetor】(四)

[复制链接]

尚未签到

发表于 2017-5-3 08:02:16 | 显示全部楼层 |阅读模式
  之前我们讲的都是简单的返回文本到浏览器,例如下面将会返回hello word到浏览器

# coding:utf-8
import web
urls=(
'/','index'
)
app=web.application(urls,globals())
class index:
def GET(self):
return 'hello word!'
if __name__ == '__main__':
app.run()
  当然我们也可以返回html到浏览器,例如

# coding:utf-8
import web
urls=(
'/','index'
)
app=web.application(urls,globals())
class index:
def GET(self):
return '''
<html>
<head>
<title>hello word</title>
</head>
<body>
<p>hello word!</p>
</body>
</html>
'''
if __name__ == '__main__':
app.run()
  显然我们不可能用上述方法来开发web应用,有没有更加优雅的方法。
  所有的web框架都提供有template技术来实现复杂的web页面webpy也不例外。
  在工程目录下建立templates目录(你也可以叫其他名字),这个目录用于存储我们所有的模板文件,
  我们在下面建立一个叫做index.html文件内容如下

$def with (name) #这里定义传递进来的参数,后面会提到
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>hello word</title>
</head>
<body>
<p>hello word!$name</p>
</body>
</html>
   现在我们就可以使用这个模板了

# coding:utf-8
import web
urls=(
'/','index'
)
app=web.application(urls,globals())
render=web.template.render('templates')#模板目录
class index:
def GET(self):
return render.index('webpy')#使用index.html模板,传递参数'webpy'(这里会查找templates下第一个匹配上index.*的文件,所以我们的模板文件其实可以使用任何扩展名结尾)
if __name__ == '__main__':
app.run()
   我们也可以指定具体的模板文件

index = web.template.frender('templates/index.html')
return index('webpy')
  我们也可以直接使用字符串为模板

template = '''
$def with (name)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>hello word</title>
</head>
<body>
<p>hello word!$name</p>
</body>
</html>
'''
index = web.template.Template(template)
return index('webpy')
  模板种允许使用python表达式,所有表达式都要以$符号开头

Look, a $string.
Hark, an ${arbitrary + expression}.
Gawk, a $dictionary[key].function('argument').
Cool, a $(limit)ing.
   还可以声明变量

$ bug = get_bug(id)
<h1>$bug.title</h1>
<div>
$bug.description
<div>
  模板变量默认是做了escape转义的

$str='1 < 2'
<p>$str</p>
输出则为:
<p>1 &lt; 2</p>
   若想不被转义则需要在$后面加行:号

$str='1 < 2'
<p>$:str</p>
输出则为:
<p>1 < 2</p>
   所有表达式都要以$开头那么要出$就需要用$$

$$50
输出为
$50
  模板种注释为$#开头

$#我是注释
<p>hello word</p>
  控制结构

$for i in range(10):
I like $i
$for i in range(10): I like $i
$while a:
hello $a.pop()
$if times > max:
Stop! In the name of love.
$else:
Keep on, you can do it.
   在循环中可以访问的一些内置变量

loop.index: the iteration of the loop (1-indexed)
loop.index0: the iteration of the loop (0-indexed)
loop.first: True if first iteration
loop.last: True if last iteration
loop.odd: True if an odd iteration
loop.even: True if an even iteration
loop.parity: "odd" or "even" depending on which is true
loop.parent: the loop above this in nested loops
  例如下面

<table>
$for c in ["a", "b", "c", "d"]:
<tr class="$loop.parity">
<td>$loop.index</td>
<td>$c</td>
</tr>
</table>
  你还可以定义方法

$def say_hello(name='world'):
Hello $name!
$say_hello('web.py')
$say_hello()
   还可以植入python代码,使用$code:
  


$code:
x = "you can write any python code here"
y = x.title()
z = len(x + y)
def limit(s, width=10):
"""limits a string to the given width"""
if len(s) >= width:
return s[:width] + "..."
else:
return s
And we are back to template.
The variables defined in the code block can be used here.
For example, $limit(x)
  当然也可以使用layout
  


render = web.template.render('templates/', base='layout')
  上面会使用 templates/layout.html 作为 layout
  layout.html内容:
  


$def with (content)
<html>
<head>
<title>Foo</title>
</head>
<body>
$:content
</body>
</html>
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-372285-1-1.html 上篇帖子: 集群环境日志抓取的python脚本 下篇帖子: 使用python发送html的邮件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表