def render_template(out, name, context, templates_dir, prefix=None): """ Render a template using tenjin. out: a file-like object name: name of the template context: dictionary of variables to pass to the template prefix: optional prefix for embedding (for other languages than python) """ # support "::" syntax pp = [ tenjin.PrefixedLinePreprocessor( prefix=prefix) if prefix else tenjin.PrefixedLinePreprocessor() ] # disable HTML escaping template_globals = {"to_str": str, "escape": str} if templates_dir: engine = TemplateEngine(path=[templates_dir], pp=pp, cache=False) else: engine = TemplateEngine(pp=pp, cache=False) out.write(engine.render(name, context, template_globals)) if 'KD_DICT' in context: return context['KD_DICT'] if 'P4TBL_TYPES' in context: return context['P4TBL_TYPES'] else: return None
def render_template(out, name, path, context, prefix = None): """ Render a template using tenjin. out: a file-like object name: name of the template path: array of directories to search for the template context: dictionary of variables to pass to the template prefix: optional prefix to use for embedding (for other languages than python) """ pp = [ tenjin.PrefixedLinePreprocessor(prefix=prefix) if prefix else tenjin.PrefixedLinePreprocessor() ] # support "::" syntax template_globals = { "to_str": str, "escape": str } # disable HTML escaping engine = TemplateEngine(path=path, pp=pp) out.write(engine.render(name, context, template_globals))
def render_template(out, name, path, context): """ Render a template using tenjin. out: a file-like object name: name of the template path: array of directories to search for the template context: dictionary of variables to pass to the template """ pp = [tenjin.PrefixedLinePreprocessor()] # support "::" syntax template_globals = {"to_str": str, "escape": str} # disable HTML escaping engine = TemplateEngine(path=path, pp=pp) out.write(engine.render(name, context, template_globals))
def render_template(out, name, context): """ Render a template using tenjin. out: a file-like object name: name of the template context: dictionary of variables to pass to the template """ path = [ os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates') ] pp = [tenjin.PrefixedLinePreprocessor()] # support "::" syntax template_globals = {"to_str": str, "escape": str} # disable HTML escaping engine = TemplateEngine(path=path, pp=pp) out.write(engine.render(name, context, template_globals))
def TempaltingEngine(self, contextdict, templatefile): #template preprocessor Arguments Parser pp = [ tenjin.TemplatePreprocessor(), # same as preprocess=True #tenjin.TrimPreprocessor(), # trim spaces before tags tenjin.PrefixedLinePreprocessor( ), # convert ':: ...' into '<?py ... ?>' ] engine = tenjin.Engine( pp=pp, path=[self.templatefilepath], cache=False, # Do not do any HTML escaping or other special magic escapefunc="str", tostrfunc="str") # render template with context data render = engine.render(templatefile, contextdict) return render
def _(self): input = r""" <ul> :: i = 0 <?py for item in items: ?> :: i += 1 <li>${item}</li> <?py #endfor ?> </ul> """[1:] expected = r""" <ul> <?py i = 0 ?> <?py for item in items: ?> <?py i += 1 ?> <li>${item}</li> <?py #endfor ?> </ul> """[1:] pp = tenjin.PrefixedLinePreprocessor() ok(pp(input)) == expected
import tenjin from tenjin.helpers import * pp = [tenjin.PrefixedLinePreprocessor()] engine = tenjin.Engine(pp=pp) context = {'items': ["Haruhi", "Mikuru", "Yuki"]} html = engine.render('example.pyhtml', context) print(html)