现在的位置: 首页 > 综合 > 正文

cheetah实例,涉及HTML表格循环以及中文

2013年03月22日 ⁄ 综合 ⁄ 共 757字 ⁄ 字号 评论关闭

1、先写一个模板文件"1.tmpl"

注意:GBK大概是操作系统默认编码格式,我的主机是windows XP系统,设为utf-8报错,设为GBK正常。

#encoding GBK
#set $current_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
<meta http-equiv="Content-Type" content="text/html;charset=utf-8 "/> 
<html>
<title>$title</title>
<body>
<h1>$subject</h1>
<table>
<tr>
 <td>
时间
</td>
 <td>$current_time</td>
</tr>
#for $item in $state 
<tr>
<td>$item.name</td>
<td>$item.value</td>
</tr>
#end for
</table>
</body>
</html>

2、写个py程序,使用模板输出HTML文件

# -*- coding: utf8 -*-

from Cheetah.Template import Template

f = open('1.html','w')
template_file = '1.tmpl'
namespace = {'title':'report','subject':'sss','state': [{'name':'磁盘剩余空间','value':'425T'},{'name':'进程状态','value':'正常运行'}]}
t = Template(file=template_file, searchList=[namespace])
print t
f.write(str(t))

3、执行py文件

得到“1.html”文件即为所要的结果。

抱歉!评论已关闭.