Commit 9aaf2e1d by Chris Jerdonek

Moved Template() template argument to render() argument.

parent 315edcd6
...@@ -64,8 +64,10 @@ def main(sys_argv): ...@@ -64,8 +64,10 @@ def main(sys_argv):
except IOError: except IOError:
context = json.loads(context) context = json.loads(context)
renderer = Renderer(template) renderer = Renderer()
print(renderer.render(context))
rendered = renderer.render(template, context)
print rendered
if __name__=='__main__': if __name__=='__main__':
......
...@@ -18,5 +18,5 @@ def render(template, context=None, **kwargs): ...@@ -18,5 +18,5 @@ def render(template, context=None, **kwargs):
Return the given template string rendered using the given context. Return the given template string rendered using the given context.
""" """
renderer = Renderer(template) renderer = Renderer()
return renderer.render(context, **kwargs) return renderer.render(template, context, **kwargs)
...@@ -22,17 +22,14 @@ except ImportError: ...@@ -22,17 +22,14 @@ except ImportError:
class Renderer(object): class Renderer(object):
def __init__(self, template=None, load_template=None, output_encoding=None, escape=None, # TODO: change load_template to load_partial.
def __init__(self, load_template=None, output_encoding=None, escape=None,
default_encoding=None, decode_errors='strict'): default_encoding=None, decode_errors='strict'):
""" """
Construct a Template instance. Construct an instance.
Arguments: Arguments:
template: a template string that is either unicode, or of type
str and encoded using the encoding named by the default_encoding
keyword argument.
load_template: the function for loading partials. The function should load_template: the function for loading partials. The function should
accept a single template_name parameter and return a template as accept a single template_name parameter and return a template as
a string. Defaults to the default Loader's load_template() method. a string. Defaults to the default Loader's load_template() method.
...@@ -84,7 +81,6 @@ class Renderer(object): ...@@ -84,7 +81,6 @@ class Renderer(object):
self.escape = escape self.escape = escape
self.load_template = load_template self.load_template = load_template
self.output_encoding = output_encoding self.output_encoding = output_encoding
self.template = template
def _unicode_and_escape(self, s): def _unicode_and_escape(self, s):
if not isinstance(s, unicode): if not isinstance(s, unicode):
...@@ -146,9 +142,9 @@ class Renderer(object): ...@@ -146,9 +142,9 @@ class Renderer(object):
escape=self._unicode_and_escape) escape=self._unicode_and_escape)
return engine return engine
def render(self, context=None, **kwargs): def render(self, template, context=None, **kwargs):
""" """
Return the template rendered using the given context. Render the given template using the given context.
The return value is a unicode string, unless the output_encoding The return value is a unicode string, unless the output_encoding
attribute has been set to a non-None value, in which case the attribute has been set to a non-None value, in which case the
...@@ -160,6 +156,10 @@ class Renderer(object): ...@@ -160,6 +156,10 @@ class Renderer(object):
Arguments: Arguments:
template: a template string that is either unicode, or of type
str and encoded using the encoding named by the default_encoding
keyword argument.
context: a dictionary, Context, or object (e.g. a View instance). context: a dictionary, Context, or object (e.g. a View instance).
**kwargs: additional key values to add to the context when rendering. **kwargs: additional key values to add to the context when rendering.
...@@ -169,13 +169,12 @@ class Renderer(object): ...@@ -169,13 +169,12 @@ class Renderer(object):
engine = self._make_render_engine() engine = self._make_render_engine()
context = self._make_context(context, **kwargs) context = self._make_context(context, **kwargs)
template = self.template
if not isinstance(template, unicode): if not isinstance(template, unicode):
template = self.unicode(template) template = self.unicode(template)
result = engine.render(template, context) rendered = engine.render(template, context)
if self.output_encoding is not None: if self.output_encoding is not None:
result = result.encode(self.output_encoding) rendered = rendered.encode(self.output_encoding)
return result return rendered
...@@ -98,9 +98,9 @@ class View(object): ...@@ -98,9 +98,9 @@ class View(object):
Return the view rendered using the current context. Return the view rendered using the current context.
""" """
renderer = Renderer(self.get_template(), self.load_template, output_encoding=encoding, template = self.get_template()
escape=escape) renderer = Renderer(self.load_template, output_encoding=encoding, escape=escape)
return renderer.render(self.context) return renderer.render(template, self.context)
def get(self, key, default=None): def get(self, key, default=None):
return self.context.get(key, default) return self.context.get(key, default)
......
...@@ -28,6 +28,9 @@ class RendererTestCase(unittest.TestCase): ...@@ -28,6 +28,9 @@ class RendererTestCase(unittest.TestCase):
def tearDown(self): def tearDown(self):
self._restore_markupsafe() self._restore_markupsafe()
def _renderer(self):
return Renderer()
def _was_markupsafe_imported(self): def _was_markupsafe_imported(self):
return bool(self.original_markupsafe) return bool(self.original_markupsafe)
...@@ -52,8 +55,8 @@ class RendererTestCase(unittest.TestCase): ...@@ -52,8 +55,8 @@ class RendererTestCase(unittest.TestCase):
self.assertEquals(bool(markupsafe), self._was_markupsafe_imported()) self.assertEquals(bool(markupsafe), self._was_markupsafe_imported())
def test_init__escape__default_without_markupsafe(self): def test_init__escape__default_without_markupsafe(self):
template = Renderer() renderer = Renderer()
self.assertEquals(template.escape(">'"), ">'") self.assertEquals(renderer.escape(">'"), ">'")
def test_init__escape__default_with_markupsafe(self): def test_init__escape__default_with_markupsafe(self):
if not self._was_markupsafe_imported(): if not self._was_markupsafe_imported():
...@@ -61,73 +64,73 @@ class RendererTestCase(unittest.TestCase): ...@@ -61,73 +64,73 @@ class RendererTestCase(unittest.TestCase):
return return
self._restore_markupsafe() self._restore_markupsafe()
template = Renderer() renderer = Renderer()
self.assertEquals(template.escape(">'"), ">'") self.assertEquals(renderer.escape(">'"), ">'")
def test_init__escape(self): def test_init__escape(self):
escape = lambda s: "foo" + s escape = lambda s: "foo" + s
template = Renderer(escape=escape) renderer = Renderer(escape=escape)
self.assertEquals(template.escape("bar"), "foobar") self.assertEquals(renderer.escape("bar"), "foobar")
def test_init__default_encoding__default(self): def test_init__default_encoding__default(self):
""" """
Check the default value. Check the default value.
""" """
template = Renderer() renderer = Renderer()
self.assertEquals(template.default_encoding, sys.getdefaultencoding()) self.assertEquals(renderer.default_encoding, sys.getdefaultencoding())
def test_init__default_encoding(self): def test_init__default_encoding(self):
""" """
Check that the constructor sets the attribute correctly. Check that the constructor sets the attribute correctly.
""" """
template = Renderer(default_encoding="foo") renderer = Renderer(default_encoding="foo")
self.assertEquals(template.default_encoding, "foo") self.assertEquals(renderer.default_encoding, "foo")
def test_init__decode_errors__default(self): def test_init__decode_errors__default(self):
""" """
Check the default value. Check the default value.
""" """
template = Renderer() renderer = Renderer()
self.assertEquals(template.decode_errors, 'strict') self.assertEquals(renderer.decode_errors, 'strict')
def test_init__decode_errors(self): def test_init__decode_errors(self):
""" """
Check that the constructor sets the attribute correctly. Check that the constructor sets the attribute correctly.
""" """
template = Renderer(decode_errors="foo") renderer = Renderer(decode_errors="foo")
self.assertEquals(template.decode_errors, "foo") self.assertEquals(renderer.decode_errors, "foo")
def test_unicode(self): def test_unicode(self):
template = Renderer() renderer = Renderer()
actual = template.literal("abc") actual = renderer.literal("abc")
self.assertEquals(actual, "abc") self.assertEquals(actual, "abc")
self.assertEquals(type(actual), unicode) self.assertEquals(type(actual), unicode)
def test_unicode__default_encoding(self): def test_unicode__default_encoding(self):
template = Renderer() renderer = Renderer()
s = "é" s = "é"
template.default_encoding = "ascii" renderer.default_encoding = "ascii"
self.assertRaises(UnicodeDecodeError, template.unicode, s) self.assertRaises(UnicodeDecodeError, renderer.unicode, s)
template.default_encoding = "utf-8" renderer.default_encoding = "utf-8"
self.assertEquals(template.unicode(s), u"é") self.assertEquals(renderer.unicode(s), u"é")
def test_unicode__decode_errors(self): def test_unicode__decode_errors(self):
template = Renderer() renderer = Renderer()
s = "é" s = "é"
template.default_encoding = "ascii" renderer.default_encoding = "ascii"
template.decode_errors = "strict" renderer.decode_errors = "strict"
self.assertRaises(UnicodeDecodeError, template.unicode, s) self.assertRaises(UnicodeDecodeError, renderer.unicode, s)
template.decode_errors = "replace" renderer.decode_errors = "replace"
# U+FFFD is the official Unicode replacement character. # U+FFFD is the official Unicode replacement character.
self.assertEquals(template.unicode(s), u'\ufffd\ufffd') self.assertEquals(renderer.unicode(s), u'\ufffd\ufffd')
def test_literal__with_markupsafe(self): def test_literal__with_markupsafe(self):
if not self._was_markupsafe_imported(): if not self._was_markupsafe_imported():
...@@ -135,39 +138,39 @@ class RendererTestCase(unittest.TestCase): ...@@ -135,39 +138,39 @@ class RendererTestCase(unittest.TestCase):
return return
self._restore_markupsafe() self._restore_markupsafe()
template = Renderer() _renderer = Renderer()
template.default_encoding = "utf_8" _renderer.default_encoding = "utf_8"
# Check the standard case. # Check the standard case.
actual = template.literal("abc") actual = _renderer.literal("abc")
self.assertEquals(actual, "abc") self.assertEquals(actual, "abc")
self.assertEquals(type(actual), renderer.markupsafe.Markup) self.assertEquals(type(actual), renderer.markupsafe.Markup)
s = "é" s = "é"
# Check that markupsafe respects default_encoding. # Check that markupsafe respects default_encoding.
self.assertEquals(template.literal(s), u"é") self.assertEquals(_renderer.literal(s), u"é")
template.default_encoding = "ascii" _renderer.default_encoding = "ascii"
self.assertRaises(UnicodeDecodeError, template.literal, s) self.assertRaises(UnicodeDecodeError, _renderer.literal, s)
# Check that markupsafe respects decode_errors. # Check that markupsafe respects decode_errors.
template.decode_errors = "replace" _renderer.decode_errors = "replace"
self.assertEquals(template.literal(s), u'\ufffd\ufffd') self.assertEquals(_renderer.literal(s), u'\ufffd\ufffd')
def test_render__unicode(self): def test_render__unicode(self):
template = Renderer(u'foo') renderer = Renderer()
actual = template.render() actual = renderer.render(u'foo')
self.assertTrue(isinstance(actual, unicode)) self.assertTrue(isinstance(actual, unicode))
self.assertEquals(actual, u'foo') self.assertEquals(actual, u'foo')
def test_render__str(self): def test_render__str(self):
template = Renderer('foo') renderer = Renderer()
actual = template.render() actual = renderer.render('foo')
self.assertTrue(isinstance(actual, unicode)) self.assertTrue(isinstance(actual, unicode))
self.assertEquals(actual, 'foo') self.assertEquals(actual, 'foo')
def test_render__non_ascii_character(self): def test_render__non_ascii_character(self):
template = Renderer(u'Poincaré') renderer = Renderer()
actual = template.render() actual = renderer.render(u'Poincaré')
self.assertTrue(isinstance(actual, unicode)) self.assertTrue(isinstance(actual, unicode))
self.assertEquals(actual, u'Poincaré') self.assertEquals(actual, u'Poincaré')
...@@ -176,32 +179,33 @@ class RendererTestCase(unittest.TestCase): ...@@ -176,32 +179,33 @@ class RendererTestCase(unittest.TestCase):
Test render(): passing a context. Test render(): passing a context.
""" """
template = Renderer('Hi {{person}}') renderer = Renderer()
self.assertEquals(template.render({'person': 'Mom'}), 'Hi Mom') self.assertEquals(renderer.render('Hi {{person}}', {'person': 'Mom'}), 'Hi Mom')
def test_render__context_and_kwargs(self): def test_render__context_and_kwargs(self):
""" """
Test render(): passing a context and **kwargs. Test render(): passing a context and **kwargs.
""" """
template = Renderer('Hi {{person1}} and {{person2}}') renderer = Renderer()
self.assertEquals(template.render({'person1': 'Mom'}, person2='Dad'), 'Hi Mom and Dad') template = 'Hi {{person1}} and {{person2}}'
self.assertEquals(renderer.render(template, {'person1': 'Mom'}, person2='Dad'), 'Hi Mom and Dad')
def test_render__kwargs_and_no_context(self): def test_render__kwargs_and_no_context(self):
""" """
Test render(): passing **kwargs and no context. Test render(): passing **kwargs and no context.
""" """
template = Renderer('Hi {{person}}') renderer = Renderer()
self.assertEquals(template.render(person='Mom'), 'Hi Mom') self.assertEquals(renderer.render('Hi {{person}}', person='Mom'), 'Hi Mom')
def test_render__context_and_kwargs__precedence(self): def test_render__context_and_kwargs__precedence(self):
""" """
Test render(): **kwargs takes precedence over context. Test render(): **kwargs takes precedence over context.
""" """
template = Renderer('Hi {{person}}') renderer = Renderer()
self.assertEquals(template.render({'person': 'Mom'}, person='Dad'), 'Hi Dad') self.assertEquals(renderer.render('Hi {{person}}', {'person': 'Mom'}, person='Dad'), 'Hi Dad')
def test_render__kwargs_does_not_modify_context(self): def test_render__kwargs_does_not_modify_context(self):
""" """
...@@ -209,14 +213,14 @@ class RendererTestCase(unittest.TestCase): ...@@ -209,14 +213,14 @@ class RendererTestCase(unittest.TestCase):
""" """
context = {} context = {}
template = Renderer('Hi {{person}}') renderer = Renderer()
template.render(context=context, foo="bar") renderer.render('Hi {{person}}', context=context, foo="bar")
self.assertEquals(context, {}) self.assertEquals(context, {})
def test_render__output_encoding(self): def test_render__output_encoding(self):
template = Renderer(u'Poincaré') renderer = Renderer()
template.output_encoding = 'utf-8' renderer.output_encoding = 'utf-8'
actual = template.render() actual = renderer.render(u'Poincaré')
self.assertTrue(isinstance(actual, str)) self.assertTrue(isinstance(actual, str))
self.assertEquals(actual, 'Poincaré') self.assertEquals(actual, 'Poincaré')
...@@ -225,15 +229,16 @@ class RendererTestCase(unittest.TestCase): ...@@ -225,15 +229,16 @@ class RendererTestCase(unittest.TestCase):
Test passing a non-unicode template with non-ascii characters. Test passing a non-unicode template with non-ascii characters.
""" """
template = Renderer("déf", output_encoding="utf-8") renderer = Renderer(output_encoding="utf-8")
template = "déf"
# Check that decode_errors and default_encoding are both respected. # Check that decode_errors and default_encoding are both respected.
template.decode_errors = 'ignore' renderer.decode_errors = 'ignore'
template.default_encoding = 'ascii' renderer.default_encoding = 'ascii'
self.assertEquals(template.render(), "df") self.assertEquals(renderer.render(template), "df")
template.default_encoding = 'utf_8' renderer.default_encoding = 'utf_8'
self.assertEquals(template.render(), "déf") self.assertEquals(renderer.render(template), "déf")
# By testing that Renderer.render() constructs the RenderEngine instance # By testing that Renderer.render() constructs the RenderEngine instance
# correctly, we no longer need to test the rendering code paths through # correctly, we no longer need to test the rendering code paths through
......
...@@ -21,7 +21,8 @@ class TestSimple(unittest.TestCase): ...@@ -21,7 +21,8 @@ class TestSimple(unittest.TestCase):
def test_empty_context(self): def test_empty_context(self):
view = ComplexView() view = ComplexView()
self.assertEquals(pystache.Renderer('{{#empty_list}}Shouldnt see me {{/empty_list}}{{^empty_list}}Should see me{{/empty_list}}', view).render(), "Should see me") template = '{{#empty_list}}Shouldnt see me {{/empty_list}}{{^empty_list}}Should see me{{/empty_list}}'
self.assertEquals(pystache.Renderer().render(template), "Should see me")
def test_callables(self): def test_callables(self):
view = Lambdas() view = Lambdas()
...@@ -38,8 +39,8 @@ class TestSimple(unittest.TestCase): ...@@ -38,8 +39,8 @@ class TestSimple(unittest.TestCase):
def test_non_existent_value_renders_blank(self): def test_non_existent_value_renders_blank(self):
view = Simple() view = Simple()
template = '{{not_set}} {{blank}}'
self.assertEquals(pystache.Renderer('{{not_set}} {{blank}}', view).render(), ' ') self.assertEquals(pystache.Renderer().render(template), ' ')
def test_template_partial_extension(self): def test_template_partial_extension(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment