def __init__(self, text='', filename='', lookup=None): if filename: text = open(filename, 'r').read() self.source = text self.parser = TemplateParser(lookup) self.module_source = self.parser.translate(text, filename) self.module_id = "memory:" + hex(id(self)) self.code = compile(self.module_source, self.module_id, 'exec') self.module = ModuleType(self.module_id)
class Template(object): def __init__(self, text='', filename='', lookup=None): if filename: text = open(filename, 'r').read() self.source = text self.parser = TemplateParser(lookup) self.module_source = self.parser.translate(text, filename) self.module_id = "memory:" + hex(id(self)) self.code = compile(self.module_source, self.module_id, 'exec') self.module = ModuleType(self.module_id) def render(self, **kwargs): self.module.__dict__.update(kwargs) exec self.code in self.module.__dict__, self.module.__dict__ self.module.out = TemplateOutput() self.module.body() return self.module.out.output