Commit aaed5225 by Chris Jerdonek

Renamed the loader argument of Renderer.__init__() to "partials".

parent e256e732
...@@ -29,38 +29,37 @@ class Renderer(object): ...@@ -29,38 +29,37 @@ class Renderer(object):
This class supports several rendering options which are described in This class supports several rendering options which are described in
the constructor's docstring. Among these, the constructor supports the constructor's docstring. Among these, the constructor supports
passing a custom template loader. passing a custom partial loader.
Here is an example of passing a custom template loader to render a Here is an example of rendering a template using a custom partial loader
template using partials loaded from a string-string dictionary. that loads partials loaded from a string-string dictionary.
>>> partials = {'partial': 'Hello, {{thing}}!'} >>> partials = {'partial': 'Hello, {{thing}}!'}
>>> renderer = Renderer(loader=partials) >>> renderer = Renderer(partials=partials)
>>> renderer.render('{{>partial}}', {'thing': 'world'}) >>> renderer.render('{{>partial}}', {'thing': 'world'})
u'Hello, world!' u'Hello, world!'
""" """
# TODO: rename the loader argument to "partials". def __init__(self, file_encoding=None, default_encoding=None,
def __init__(self, loader=None, file_encoding=None, default_encoding=None,
decode_errors='strict', search_dirs=None, file_extension=None, decode_errors='strict', search_dirs=None, file_extension=None,
escape=None): escape=None, partials=None):
""" """
Construct an instance. Construct an instance.
Arguments: Arguments:
loader: an object (e.g. pystache.Loader or dictionary) for custom partials: an object (e.g. pystache.Loader or dictionary) for
partial loading during the rendering process. custom partial loading during the rendering process.
The loader should have a get() method that accepts a string The object should have a get() method that accepts a string
and returns the corresponding template as a string, preferably and returns the corresponding template as a string, preferably
as a unicode string. If there is no template with that name, as a unicode string. If there is no template with that name,
the method should either return None (as dict.get() does) or the get() method should either return None (as dict.get() does)
raise an exception. or raise an exception.
If this argument is None, partial loading takes place using If this argument is None, the rendering process will use
the normal procedure of reading templates from the file system the normal procedure of locating and reading templates from
using the Loader-related instance attributes (search_dirs, the file system -- using the Loader-related instance attributes
file_encoding, etc). like search_dirs, file_encoding, etc.
escape: the function used to escape variable tag values when escape: the function used to escape variable tag values when
rendering a template. The function should accept a unicode rendering a template. The function should accept a unicode
...@@ -124,8 +123,7 @@ class Renderer(object): ...@@ -124,8 +123,7 @@ class Renderer(object):
self.escape = escape self.escape = escape
self.file_encoding = file_encoding self.file_encoding = file_encoding
self.file_extension = file_extension self.file_extension = file_extension
# TODO: rename self.loader to self.partials. self.partials = partials
self.loader = loader
self.search_dirs = search_dirs self.search_dirs = search_dirs
def _to_unicode_soft(self, s): def _to_unicode_soft(self, s):
...@@ -209,17 +207,17 @@ class Renderer(object): ...@@ -209,17 +207,17 @@ class Renderer(object):
Return the load_partial function to pass to RenderEngine.__init__(). Return the load_partial function to pass to RenderEngine.__init__().
""" """
if self.loader is None: if self.partials is None:
loader = self._make_loader() loader = self._make_loader()
return loader.get return loader.get
# Otherwise, create a load_partial function from the custom loader # Otherwise, create a load_partial function from the custom loader
# that satisfies RenderEngine requirements (and that provides a # that satisfies RenderEngine requirements (and that provides a
# nicer exception, etc). # nicer exception, etc).
loader = self.loader get_partial = self.partials.get
def load_partial(name): def load_partial(name):
template = loader.get(name) template = get_partial(name)
if template is None: if template is None:
# TODO: make a TemplateNotFoundException type that provides # TODO: make a TemplateNotFoundException type that provides
......
...@@ -60,7 +60,7 @@ class View(object): ...@@ -60,7 +60,7 @@ class View(object):
# instantiation some of the attributes on which the Renderer # instantiation some of the attributes on which the Renderer
# depends. This lets users set the template_extension attribute, # depends. This lets users set the template_extension attribute,
# etc. after View.__init__() has already been called. # etc. after View.__init__() has already been called.
renderer = Renderer(loader=self._loader, renderer = Renderer(partials=self._loader,
file_encoding=self.template_encoding, file_encoding=self.template_encoding,
search_dirs=self.template_path, search_dirs=self.template_path,
file_extension=self.template_extension) file_extension=self.template_extension)
......
...@@ -23,24 +23,21 @@ class RendererInitTestCase(unittest.TestCase): ...@@ -23,24 +23,21 @@ class RendererInitTestCase(unittest.TestCase):
""" """
def test_loader(self): def test_partials__default(self):
""" """
Test that the loader attribute is set correctly. Test that the default loader is constructed correctly.
""" """
loader = {'foo': 'bar'} renderer = Renderer()
renderer = Renderer(loader=loader) self.assertTrue(renderer.partials is None)
self.assertEquals(renderer.loader, {'foo': 'bar'})
def test_loader__default(self): def test_partials(self):
""" """
Test that the default loader is constructed correctly. Test that the loader attribute is set correctly.
""" """
renderer = Renderer() renderer = Renderer(partials={'foo': 'bar'})
actual = renderer.loader self.assertEquals(renderer.partials, {'foo': 'bar'})
self.assertTrue(renderer.loader is None)
def test_escape__default(self): def test_escape__default(self):
escape = Renderer().escape escape = Renderer().escape
...@@ -382,8 +379,8 @@ class RendererTestCase(unittest.TestCase): ...@@ -382,8 +379,8 @@ class RendererTestCase(unittest.TestCase):
Test the _make_load_partial() method. Test the _make_load_partial() method.
""" """
partials = {'foo': 'bar'} renderer = Renderer()
renderer = Renderer(loader=partials) renderer.partials = {'foo': 'bar'}
load_partial = renderer._make_load_partial() load_partial = renderer._make_load_partial()
actual = load_partial('foo') actual = load_partial('foo')
...@@ -398,12 +395,12 @@ class RendererTestCase(unittest.TestCase): ...@@ -398,12 +395,12 @@ class RendererTestCase(unittest.TestCase):
""" """
renderer = Renderer() renderer = Renderer()
renderer.loader = {'partial': 'foo'} renderer.partials = {'partial': 'foo'}
load_partial = renderer._make_load_partial() load_partial = renderer._make_load_partial()
self.assertEquals(load_partial("partial"), "foo") self.assertEquals(load_partial("partial"), "foo")
# Now with a value that is already unicode. # Now with a value that is already unicode.
renderer.loader = {'partial': u'foo'} renderer.partials = {'partial': u'foo'}
load_partial = renderer._make_load_partial() load_partial = renderer._make_load_partial()
# If the next line failed, we would get the following error: # If the next line failed, we would get the following error:
# TypeError: decoding Unicode is not supported # TypeError: decoding Unicode is not supported
...@@ -443,7 +440,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase): ...@@ -443,7 +440,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase):
renderer = Renderer() renderer = Renderer()
renderer.default_encoding = 'ascii' renderer.default_encoding = 'ascii'
renderer.loader = {'str': 'foo', 'subclass': MyUnicode('abc')} renderer.partials = {'str': 'foo', 'subclass': MyUnicode('abc')}
engine = renderer._make_render_engine() engine = renderer._make_render_engine()
...@@ -462,7 +459,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase): ...@@ -462,7 +459,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase):
""" """
renderer = Renderer() renderer = Renderer()
renderer.loader = {} renderer.partials = {}
engine = renderer._make_render_engine() engine = renderer._make_render_engine()
load_partial = engine.load_partial load_partial = engine.load_partial
......
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