Skip to content

Commit

Permalink
support Pybars 0.8+ which raises a PybarsError now instead of a KeyEr…
Browse files Browse the repository at this point in the history
…ror on partials not found
  • Loading branch information
schuyler1d committed Jun 18, 2015
1 parent de32ac6 commit 58b7e42
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions djangobars/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
except NameError:
strtype = str

class FakeError(Exception):
pass

try:
from pybars import PybarsError
except ImportError:
PybarsError = FakeError


class PartialList():
"""
If the code references a partial in the template directory, then
Expand Down Expand Up @@ -38,7 +47,7 @@ def __setitem__(self, key, val):
self.partials[key] = val


class HandlebarsTemplate (object):
class HandlebarsTemplate(object):
PARTIALS = {}

def __init__(self, template_string, origin=None,
Expand All @@ -53,6 +62,11 @@ def __init__(self, template_string, origin=None,
if is_partial:
self.partials[name] = self.fn

def _compile_partial(self, partial_name):
from djangobars.template.loader import get_template
template = get_template(partial_name)
self.partials[partial_name] = template.fn

def render(self, context):
if hasattr(context, 'render_context'):
context.render_context.push()
Expand All @@ -62,11 +76,18 @@ def render(self, context):
context, helpers=self.helpers, partials=self.partials)
return strtype(s)
except KeyError as e:
from djangobars.template.loader import get_template
partial_name = strtype(e).strip("'")
template = get_template(partial_name)
self.partials[partial_name] = template.fn
self._compile_partial(partial_name)
return self.render(context)
except PybarsError as e:
#support for Pybars 0.8+
err = e.message
if err.startswith('Partial') and err.find('not defined'):
partial_name = err[err.index('"')+1:err.rindex('"')]
self._compile_partial(partial_name)
return self.render(context)
else:
raise
finally:
if hasattr(context, 'render_context'):
context.render_context.pop()

0 comments on commit 58b7e42

Please sign in to comment.