Commit 4a4b4ddc by Chris Jerdonek

Simplified Renderer._make_resolve_partial().

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