简单的模板引擎,简单模板引擎,#!/usr/bin/p


#!/usr/bin/python# -*- coding: utf-8 -*-import reimport copyimport collectionsreobj = re.compile(r"\$([^$]*)\$")tpldict = {}TplClass = collections.namedtuple('TplClass', ['varpos', 'tplslice'])def preproccess(tplid, tpl):    results = reobj.finditer(tpl)    obj = TplClass({}, [])    start = 0    end = 0    for i in results:        start = i.start(0)        obj.tplslice.append(tpl[end:start])        end = i.end(0)        obj.varpos.setdefault(i.group(1), []).append(len(obj.tplslice))        obj.tplslice.append(None)    obj.tplslice.append(tpl[end:])    tpldict[tplid] = objdef make(tplid, tplvars):    obj = tpldict[tplid]    tplslice = copy.copy(obj.tplslice)    for key, value in tplvars.iteritems():        for idx in obj.varpos[key]:            tplslice[idx] = value            print tplslice    return ''.join(tplslice)if __name__ == '__main__':    # preproccess    tpl = "my name is $spname$, i'm $spage$ years old"    tplid = 1    preproccess(tplid, tpl)    # make text    print make(tplid, {'spname' : 'robin928', 'spage' : '29'})

评论关闭