From 5c3a63d84ba8f82b636fbe75e4cd98ea673f6a34 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 28 Nov 2010 02:06:22 +0900 Subject: [PATCH] catch errors or missing fields when rendering template --- pystache/template.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pystache/template.py b/pystache/template.py index a87fec2a8..409bd0bae 100644 --- a/pystache/template.py +++ b/pystache/template.py @@ -110,9 +110,12 @@ class Template(object): tag, tag_type, tag_name = match.group(0, 1, 2) tag_name = tag_name.strip() - func = modifiers[tag_type] - replacement = func(self, tag_name, context) - template = template.replace(tag, replacement) + try: + func = modifiers[tag_type] + replacement = func(self, tag_name, context) + template = template.replace(tag, replacement) + except: + return "{{invalid template}}" return template @@ -133,7 +136,7 @@ class Template(object): @modifier(None) def render_unescaped(self, tag_name=None, context=None): """Render a tag without escaping it.""" - return unicode(get_or_attr(context, tag_name, '')) + return unicode(get_or_attr(context, tag_name, '{unknown field %s}' % tag_name)) # @modifier('>') # def render_partial(self, tag_name=None, context=None):