def test_search_dirs__list(self): """ Check that the search_dirs attribute is set correctly when a list. """ renderer = Renderer(search_dirs=['foo']) self.assertEquals(renderer.search_dirs, ['foo'])
def test_partials(self): """ Test that the attribute is set correctly. """ renderer = Renderer(partials={'foo': 'bar'}) self.assertEquals(renderer.partials, {'foo': 'bar'})
def render(template, context=None, **kwargs): """ Return the given template string rendered using the given context. """ renderer = Renderer() return renderer.render(template, context, **kwargs)
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_file_encoding__default(self): """ Check the file_encoding default. """ renderer = Renderer() self.assertEquals(renderer.file_encoding, renderer.default_encoding)
def test_file_encoding(self): """ Check that the file_encoding attribute is set correctly. """ renderer = Renderer(file_encoding='foo') self.assertEquals(renderer.file_encoding, 'foo')
def test_decode_errors__default(self): """ Check the default value. """ renderer = Renderer() self.assertEquals(renderer.decode_errors, 'strict')
def test_default_encoding(self): """ Check that the constructor sets the attribute correctly. """ renderer = Renderer(default_encoding="foo") self.assertEquals(renderer.default_encoding, "foo")
def test_file_extension__default(self): """ Check the file_extension default. """ renderer = Renderer() self.assertEquals(renderer.file_extension, 'mustache')
def test_escape__default(self): escape = Renderer().escape self.assertEquals(escape(">"), ">") self.assertEquals(escape('"'), """) # Single quotes are not escaped. self.assertEquals(escape("'"), "'")
def test_default_encoding__default(self): """ Check the default value. """ renderer = Renderer() self.assertEquals(renderer.default_encoding, sys.getdefaultencoding())
def render(template_name, variables, template_dir='templates', file_ext=".mustache"): """ Render a mustache template given a dict representing its variables. Args: template_name (str): the name of the template to be rendered variables (dict): a string -> any dict holding values of variables used in the template template_dir (str): the template directory, relative to the GitDox root directory. Defaults to 'templates' file_ext (str): the file extension of templates. Defaults to '.mustache' Returns: str: rendered HTML. """ template_dir = prefix + template_dir # load Mustache templates so we can reference them in our large templates templates = dict([(filename[:-len(file_ext)], open(template_dir + os.sep + filename, 'r').read()) for filename in os.listdir(template_dir) if filename.endswith(file_ext)]) renderer = Renderer(partials=templates) variables['skin_stylesheet'] = config['skin'] variables['navbar_html'] = get_menu() return renderer.render_path( template_dir + os.sep + template_name + file_ext, variables)
def test_search_dirs__default(self): """ Check the search_dirs default. """ renderer = Renderer() self.assertEquals(renderer.search_dirs, [os.curdir])
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_decode_errors(self): """ Check that the constructor sets the attribute correctly. """ renderer = Renderer(decode_errors="foo") self.assertEquals(renderer.decode_errors, "foo")
def test_render__context(self): """ Test render(): passing a context. """ renderer = Renderer() self.assertEquals(renderer.render('Hi {{person}}', {'person': 'Mom'}), 'Hi Mom')
def test_partials__default(self): """ Test the default value. """ renderer = Renderer() self.assertTrue(renderer.partials is None)
def render(template, context=None, name=None, **kwargs): """ Return the given template string rendered using the given context. """ renderer = Renderer() parsed_template = parse(template, name=name) return renderer.render(parsed_template, context, **kwargs)
def test_read__decode_errors(self): filename = 'nonascii.mustache' renderer = Renderer() self.assertRaises(UnicodeDecodeError, self._read, renderer, filename) renderer.decode_errors = 'ignore' actual = self._read(renderer, filename) self.assertEquals(actual, 'non-ascii: ')
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_make_locator__default(self): renderer = Renderer() actual = renderer.make_locator() expected = Locator() self.assertEquals(type(actual), type(expected)) self.assertEquals(actual.template_extension, expected.template_extension)
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_make_locator__return_type(self): """ Test that make_locator() returns a Locator. """ renderer = Renderer() locator = renderer.make_locator() self.assertEquals(type(locator), Locator)
def test_render_path(self): """ Test the render_path() method. """ renderer = Renderer() path = get_data_path('say_hello.mustache') actual = renderer.render_path(path, to='foo') self.assertEquals(actual, "Hello, foo")
def _engine(self): """ Create and return a default RenderEngine for testing. """ renderer = Renderer(string_encoding='utf-8', missing_tags='strict') engine = renderer._make_render_engine() return engine
def _render_template(self, content, context): if not _templates_supported: raise ToolError("Pystache must be installed in order to render files as Mustache templates") log.debug('Rendering as Mustache template') try: return Renderer(string_encoding='utf-8', file_encoding='utf-8').render(content, context) except Exception, e: raise ToolError("Failed to render content as a Mustache template: %s" % e.message)
def test_read__file_encoding(self): filename = 'nonascii.mustache' renderer = Renderer() renderer.file_encoding = 'ascii' self.assertRaises(UnicodeDecodeError, self._read, renderer, filename) renderer.file_encoding = 'utf-8' actual = self._read(renderer, filename) self.assertEquals(actual, u'non-ascii: é')
def test_render__view(self): """ Test rendering a View instance. """ renderer = Renderer() view = Simple() actual = renderer.render(view) self.assertEquals('Hi pizza!', actual)
def test_make_locator__file_extension(self): """ Test that make_locator() respects the file_extension attribute. """ renderer = Renderer() renderer.file_extension = 'foo' locator = renderer.make_locator() self.assertEquals(locator.template_extension, 'foo')