def _(self): input = self.INPUT context = {'items': ["<AAA>", "B&B"]} def fn(): pp(input, filename="foobar.pyhtml", context=context) # pp = tenjin.TemplatePreprocessor(tenjin.Preprocessor) ok(fn).not_raise() # pp = tenjin.TemplatePreprocessor(tenjin.SafePreprocessor) ok(fn).raises( tenjin.TemplateSyntaxError, "#{{item}}: '#{{}}' is not allowed with SafePreprocessor.")
def test_pp(self): if spec("'pp' paramater should be a list of preprocessor objects."): pp1 = tenjin.TemplatePreprocessor() pp2 = tenjin.TrimPreprocessor() pp3 = tenjin.JavaScriptPreprocessor(type="text/javascript") e = tenjin.Engine(pp=[pp1, pp2, pp3]) input = r""" <body> <div> <!-- #JS: render_items(items) --> <ul> <?js for (var i = 0; i < items.length; i++) { ?> <li>${i}</li> <?js } ?> </ul> <!-- #/JS --> </div> <script>#{{tenjin.JS_FUNC}}</script> </body> """[1:] expected = r""" <body> <div> <script type="text/javascript">function render_items(items){var _buf=''; _buf+='<ul>\n'; for (var i = 0; i < items.length; i++) { _buf+='<li>'+_E(i)+'</li>\n'; } _buf+='</ul>\n'; return _buf;};</script> </div> <script>function _S(x){return x==null?'':x;} function _E(x){return x==null?'':typeof(x)!=='string'?x:x.replace(/[&<>"']/g,_EF);} var _ET={'&':"&",'<':"<",'>':">",'"':""","'":"'"}; function _EF(c){return _ET[c];};</script> </body> """[1:] fname = 'tmp_123.pyhtml' f = open(fname, 'w'); f.write(input); f.close() try: t = e.get_template(fname) context = {} output = e.render(fname, context) ok (output) == expected finally: for x in glob(fname + '*'): os.unlink(x)
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): pp = tenjin.TemplatePreprocessor() ok(pp.factory) == tenjin.Preprocessor
def test_call(self): input, expected = self.INPUT, self.EXPECTED context = {'items': ["<AAA>", "B&B"]} pp = tenjin.TemplatePreprocessor() ok(pp(input, filename="foobar.rhtml", context=context)) == expected