python简单代码|Python实现的简单模板引擎功能示例

时间:2021-08-15  来源: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" ]=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 ):
          # 需要跳过结束标记
          pos_end+=2
          py_code.append(t_code)
        else :
          # 没查找到直接结束
          pos_end=tlen
          # 需要跳过

python简单代码|Python实现的简单模板引擎功能示例

http://m.bbyears.com/jiaocheng/136120.html

推荐访问:python简单吗 简单的python代码
相关阅读 猜你喜欢
本类排行 本类最新