Simple template engine function example implemented by Python, python example
This example describes the simple template engine functions implemented by Python. We will share this with you for your reference. The details are as follows:
# Coding: utf-8 _ author __= "sdm" _ author_email = 'sdmzhu3 @ gmail.com '_ date _ = "$2009-8-25 21:04:13 $" ''' pytpl templates similar to php '''import sysimport StringIOimport OS. pathimport OS # dashboard cache _ 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): ''' sets the template variable ''' self. data [name] = value; pass def get (self, name): ''' The template variable ''' t = {} return t. get (name, '') pass def tpl (self, tplname): ''' coloring template ''' 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 () Counter T: pass py_code = co Mpile (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): ''' execute this template ''' 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 ):'' 'Compile and translate template lookup <? Flag ''' tlen = len (code); flag_start = '<? 'Flag_end = '?> '# Default normal flag status = 0 I = 0 # block mark pos_end = 0 pos_start = 0 # scale into global indent = 0 py_code = [] def place_t_code (c, t_indent ): ''' basic code processing ''' global indent if (c [0] = '): return (''' * 4 * indent) + 'echo (/'% s/' % ('+ c [1:] +') 'lines = c. split ("/n") t = [] for I in lines: indent2 = indent tmp = I. strip ("/n/r") c = tmp [len (tmp)-1: len (tmp)] # determine the last character 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: # compilation acceleration 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 # Enter the code status = 1 co Ntinue else: # No pos_start = tlen t_code = 'echo ('+ repr (code [pos_end: pos_start]) +') found ') 't_code = ''' * indent * 4 + t_code py_code.append (t_code) break if status = 1: # search end mark pos_end = code. find (flag_end, I) if (pos_end>-1): # Skip <? This mark t_code = place_t_code (code [pos_start + 2: pos_end], indent) # Skip?> End tag pos_end + = 2 py_code.append (t_code) else: # No result found. Directly end pos_end = tlen # Skip <? This mark t_code = place_t_code (code [pos_start + 2: pos_end], indent) py_code.append (t_code) break status = 0 I = pos_end pass I + = 1 py_code_str = "# 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_strdef test (): tpl = Pytpl ('. /'); tpl. set ('title', 'title' 3 ') print tpl.tpl('test.html') passif _ name _ = "_ main _": test ()