Commit 4a4b4ddc by Chris Jerdonek

Simplified Renderer._make_resolve_partial().

parent bc2ca04c
...@@ -232,48 +232,49 @@ class Renderer(object): ...@@ -232,48 +232,49 @@ class Renderer(object):
return load_template return load_template
# TODO: simplify this method. def _make_load_partial(self):
def _make_resolve_partial(self):
""" """
Return the resolve_partial function to pass to RenderEngine.__init__(). Return a function that loads a partial by name.
""" """
if self.partials is None: if self.partials is None:
load_template = self._make_load_template() return self._make_load_template()
if self.missing_tags == MissingTags.strict:
return load_template
# Otherwise, ignore missing tags.
def resolve_partial(name): # Otherwise, create a function from the custom partial loader.
try:
return load_template(name)
except TemplateNotFoundError:
return u''
return resolve_partial
# Otherwise, create a resolve_partial function from the custom partial
# loader that satisfies RenderEngine requirements (and that provides
# a nicer exception, etc).
partials = self.partials partials = self.partials
if self.missing_tags == MissingTags.strict: def load_partial(name):
def on_template_none(name, partials): # TODO: consider using EAFP here instead.
raise TemplateNotFoundError("Name %s not found in partials: %s" % # http://docs.python.org/glossary.html#term-eafp
(repr(name), type(partials))) # This would mean requiring that the custom partial loader
else: # raise a KeyError on name not found.
# Otherwise, ignore missing tags.
on_template_none = lambda name, partials: u''
def resolve_partial(name):
template = partials.get(name) template = partials.get(name)
if template is None: if template is None:
return on_template_none(name, partials) raise TemplateNotFoundError("Name %s not found in partials: %s" %
(repr(name), type(partials)))
# RenderEngine requires that the return value be unicode. # RenderEngine requires that the return value be unicode.
return self._to_unicode_hard(template) return self._to_unicode_hard(template)
return load_partial
def _make_resolve_partial(self):
"""
Return the resolve_partial function to pass to RenderEngine.__init__().
"""
load_partial = self._make_load_partial()
if self.missing_tags == MissingTags.strict:
return load_partial
# Otherwise, ignore missing tags.
def resolve_partial(name):
try:
return load_partial(name)
except TemplateNotFoundError:
return u''
return resolve_partial return resolve_partial
def _make_render_engine(self): def _make_render_engine(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