def render_template(template_file,data): from htmltmpl import TemplateManager, TemplateProcessor mgr = TemplateManager() template = mgr.prepare(template_file) tproc = TemplateProcessor(global_vars=1) for key in data.keys(): tproc.set(key,data[key]) print tproc.process(template)
def prepareTemplate(self): # tmanager tries to use a compiled (pickle) version of the # .tmpl file if available. The "problem" is that it internally # reads the files in a "normal way". I should use the # TemplateCompiler class in the following way: # # templateCompiler = TemplateCompiler() # return templateCompiler.compileString(dataReceivedFromKamaeliaReader) # # and serialize this class to a tmplc file # tmanager = TemplateManager() return tmanager.prepare(self.getTemplateFileName())
def prepareTemplate(self): # tmanager tries to use a compiled (pickle) version of the # .tmpl file if available. The "problem" is that it internally # reads the files in a "normal way". I should use the # TemplateCompiler class in the following way: # # templateCompiler = TemplateCompiler() # return templateCompiler.compileString(dataReceivedFromKamaeliaReader) # # and serialize this class to a tmplc file # tmanager = TemplateManager() return tmanager.prepare(self.getTemplateFileName())
def html_template(self,template_file,data={}): """ replaces the variables in the template with values specified in a dictionary. uses the htmltmpl module. see the documentation for htmltmpl for more info. """ from htmltmpl import TemplateManager, TemplateProcessor mgr = TemplateManager() template = mgr.prepare("%s/%s" % (self.template_dir,template_file)) tproc = TemplateProcessor(html_escape=0) for key in self.__globals__.keys(): tproc.set(key,self.__globals__[key]) for key in data.keys(): tproc.set(key,data[key]) return tproc.process(template)
#!/usr/bin/env python import sys import gettext import locale sys.path.insert(0, "../..") from htmltmpl import TemplateManager, TemplateProcessor locale.setlocale(locale.LC_MESSAGES, "en_US") gettext.bindtextdomain("test", "./locale") gettext.textdomain("test") man = TemplateManager(precompile = 0, gettext = 1, debug = 1) tmpl = man.prepare("gettext.tmpl") tproc = TemplateProcessor(debug = 1) tproc.set("title", "Gettext test page") print(tproc.process(tmpl))
#!/usr/bin/env python TEST = "compiled" import sys import os sys.path.insert(0, "..") from htmltmpl import TemplateManager, TemplateProcessor man = TemplateManager(precompile = 1, debug = "debug" in sys.argv) template = man.prepare(TEST + ".tmpl") tproc = TemplateProcessor(debug = "debug" in sys.argv) ####################################################### def fill(tproc): tproc.set("title", "Template world.") tproc.set("greeting", "Hello !") tproc.set("Boys", [ { "name" : "Tomas", "age" : 19 }, { "name" : "Pavel", "age" : 34 }, { "name" : "Janek", "age" : 67 }, { "name" : "Martin", "age" : 43 }, { "name" : "Viktor", "age" : 78 }, { "name" : "Marian", "age" : 90 }, { "name" : "Prokop", "age" : 23 }, { "name" : "Honzik", "age" : 46 }, { "name" : "Brudra", "age" : 64 }, { "name" : "Marek", "age" : 54 }, { "name" : "Peter", "age" : 42 },