神剑山庄资源网 Design By www.hcban.com
本文实例讲述了Python实现的简单模板引擎功能。分享给大家供大家参考,具体如下:
#coding:utf- 8 __author__="sdm" __author_email='sdmzhu3@gmail.com' __date__ ="$2009-8-25 21:04:13$" '' ' pytpl 类似 php的模板类 '' ' import sys import StringIO import os.path import os #模 板的缓存 _tpl_cache={} class Pytpl: def __init__(self,tpl_path='./' ): self.tpl_path=tpl_path self.data={} self.output = StringIO.StringIO() pass def set(self,name,value): '' ' 设 置模板变量 '' ' self.data[name]=value; pass def get(self,name): '' ' 得 到模板变量 '' ' t={} return t.get(name, '' ) pass def tpl(self,tplname): '' ' 渲 染模板 '' ' f=self.tpl_path+tplname if not os.path.exists(f): raise Exception('tpl:[%s] is not exists' % f) mtime=os.stat(f).st_mtime if not _tpl_cache.has_key(f) or _tpl_cache[f][ 'time' ]<mtime: src_code=self.__compile__(open(f).read()) try : t=open(f+'.py' , 'w' ) t.write(src_code) t.close() except: pass py_code=compile(src_code, f+'.py' , 'exec' ) _tpl_cache[f]={'code' :py_code, 'time' :mtime} else : py_code= _tpl_cache[f]['code' ] exec(py_code, {'self' :self}, self.data) return self.output.getvalue() def execute(self,code,data,tplname): '' ' 执 行这个模板 '' ' py_file_name=tplname+'.py' f=open(py_file_name,'w' ) f.write(code) f.close() execfile(py_file_name, {'self' :self}, data) def __compile__ (self,code): '' ' 编 译模板 查找 <"/n" ) t=[] for i in lines: indent2=indent tmp=i.strip(" /n/r" ) c=tmp[len(tmp)-1 :len(tmp)] # 判定最后一个字符 if (c== '{' ): indent+=1 tmp=tmp[0 :len(tmp)- 1 ]+ ":" elif(c=='}' ): indent-=1 tmp=tmp[0 :len(tmp)- 1 ] t.append((' ' * 4 *indent2) +tmp ) return "/n" .join(t) while 1 : if i>=tlen: break c=code[i]; if status== 0 : # 编译加速 pos_start=code.find(flag_start,pos_end); if (pos_start>- 1 ): s=code[pos_end:pos_start] t_code= 'echo ( ' +repr(s)+ ')' t_code=' ' *indent* 4 +t_code if s: py_code.append(t_code) i=pos_start last_pos=i # 进入代码状态 status=1 continue else : # 没有没有找到 pos_start=tlen t_code='echo ( ' +repr(code[pos_end:pos_start])+ ' ) ' t_code=' ' *indent* 4 +t_code py_code.append(t_code) break if status== 1 : # 查找结束标记 pos_end=code.find(flag_end,i) if (pos_end>- 1 ): # 需要跳过<"#coding:utf-8/nimport sys;global echo;echo=self.output.write/n" py_code_str+="/n" .join(py_code) py_code_str=py_code_str.replace("/t" , " " ) return py_code_str def test(): tpl=Pytpl('./' ); tpl.set('title' , '标题3' ) print tpl.tpl('test.html' ) pass if __name__ == "__main__" : test()
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
标签:
Python,模板引擎
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无Python实现的简单模板引擎功能示例的评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年10月03日
2024年10月03日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】