def test_render__kwargs_does_not_modify_context(self): """ Test render(): passing **kwargs does not modify the passed context. """ context = {} renderer = Renderer() renderer.render('Hi {{person}}', context=context, foo="bar") self.assertEquals(context, {})
def test_render__object(self): """ Test rendering an object instance. """ renderer = Renderer() say_hello = SayHello() actual = renderer.render(say_hello) self.assertEquals('Hello, World', actual) actual = renderer.render(say_hello, to='Mars') self.assertEquals('Hello, Mars', actual)
def test_render__nonascii_template(self): """ Test passing a non-unicode template with non-ascii characters. """ renderer = Renderer() template = "déf" # Check that decode_errors and default_encoding are both respected. renderer.decode_errors = 'ignore' renderer.default_encoding = 'ascii' self.assertEquals(renderer.render(template), "df") renderer.default_encoding = 'utf_8' self.assertEquals(renderer.render(template), u"déf")
def pystache_render(file_in, file_out, config): """Render a pystache template. file_in is the input file path. file_out is the output file path. config is a dictionary with the context data. Note: this function will create any directories neccessary to enable writing of the output file. """ renderer = pystache.renderer.Renderer() renderer.register_formatter('u', lambda x: x.upper()) # Note: This can only be used on integer values renderer.register_formatter('hex', lambda x: str(hex(int(x)))) with open(file_in, 'r') as inp: template_data = inp.read() try: parsed_template = pystache.parser.parse(template_data, name=file_in) data = renderer.render(parsed_template, config) except pystache.common.PystacheError as e: raise SystemBuildError("Error rendering template '{}'. {}.".format( e.location, str(e))) os.makedirs(os.path.dirname(file_out), exist_ok=True) with open(file_out, 'w') as outp: outp.write(data)
def pystache_render(file_in, file_out, config): """Render a pystache template. file_in is the input file path. file_out is the output file path. config is a dictionary with the context data. Note: this function will create any directories neccessary to enable writing of the output file. """ renderer = pystache.renderer.Renderer() renderer.register_formatter('u', lambda x: x.upper()) # Note: This can only be used on integer values renderer.register_formatter('hex', lambda x: str(hex(int(x)))) with open(file_in, 'r') as inp: template_data = inp.read() try: parsed_template = pystache.parser.parse(template_data, name=file_in) data = renderer.render(parsed_template, config) except pystache.common.PystacheError as e: raise SystemBuildError("Error rendering template '{}'. {}.".format(e.location, str(e))) os.makedirs(os.path.dirname(file_out), exist_ok=True) with open(file_out, 'w') as outp: outp.write(data)
def test_render__context_and_kwargs__precedence(self): """ Test render(): **kwargs takes precedence over context. """ renderer = Renderer() self.assertEquals(renderer.render('Hi {{person}}', {'person': 'Mom'}, person='Dad'), 'Hi Dad')
def test_render__context(self): """ Test render(): passing a context. """ renderer = Renderer() self.assertEquals(renderer.render('Hi {{person}}', {'person': 'Mom'}), 'Hi Mom')
def test_render__kwargs_and_no_context(self): """ Test render(): passing **kwargs and no context. """ renderer = Renderer() self.assertEquals(renderer.render('Hi {{person}}', person='Mom'), 'Hi Mom')
def test_render__context_and_kwargs(self): """ Test render(): passing a context and **kwargs. """ renderer = Renderer() template = 'Hi {{person1}} and {{person2}}' self.assertEquals(renderer.render(template, {'person1': 'Mom'}, person2='Dad'), 'Hi Mom and Dad')
def test_render__return_type(self): """ Check that render() returns a string of type unicode. """ renderer = Renderer() rendered = renderer.render('foo') self.assertEquals(type(rendered), unicode)
def test_render__view(self): """ Test rendering a View instance. """ renderer = Renderer() view = Simple() actual = renderer.render(view) self.assertEquals('Hi pizza!', actual)
def renders_impl(self, template_content, context, at_paths=None, at_encoding=anytemplate.compat.ENCODING, **kwargs): """ Render given template string and return the result. :param template_content: Template content :param context: A dict or dict-like object to instantiate given template file :param at_paths: Template search paths :param at_encoding: Template encoding :param kwargs: Keyword arguments passed to the template engine to render templates with specific features enabled. :return: Rendered string """ renderer = self._make_renderer(at_paths, at_encoding, **kwargs) ctxs = [] if context is None else [context] return renderer.render(template_content, *ctxs)
def test_render__non_ascii_character(self): renderer = Renderer() actual = renderer.render(u'Poincaré') self.assertEquals(actual, u'Poincaré')
def test_render__str(self): renderer = Renderer() actual = renderer.render('foo') self.assertEquals(actual, 'foo')
def test_render__unicode(self): renderer = Renderer() actual = renderer.render(u'foo') self.assertEquals(actual, u'foo')