summaryrefslogtreecommitdiff
path: root/pystache/renderer.py
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-05-04 05:01:33 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-05-04 05:38:20 -0700
commitbc2ca04c18140b9eb682bb9b026ba7ed51ed45e8 (patch)
tree4ba6eacd66ad0b02364acd030abb2aacd00bf3d4 /pystache/renderer.py
parentb6c4b4af3e408d671521109880dd1b26773229c6 (diff)
downloadpystache-bc2ca04c18140b9eb682bb9b026ba7ed51ed45e8.tar.gz
Added to Renderer class support for missing_tags (not finished yet).
Support for partial loading added. Still need to support missing tags.
Diffstat (limited to 'pystache/renderer.py')
-rw-r--r--pystache/renderer.py35
1 files changed, 27 insertions, 8 deletions
diff --git a/pystache/renderer.py b/pystache/renderer.py
index 05c5e07..0c50577 100644
--- a/pystache/renderer.py
+++ b/pystache/renderer.py
@@ -8,7 +8,7 @@ This module provides a Renderer class to render templates.
import sys
from pystache import defaults
-from pystache.common import TemplateNotFoundError
+from pystache.common import TemplateNotFoundError, MissingTags
from pystache.context import ContextStack
from pystache.loader import Loader
from pystache.renderengine import RenderEngine
@@ -27,6 +27,7 @@ else:
_STRING_TYPES = (unicode, type(u"a".encode('utf-8')))
+
class Renderer(object):
"""
@@ -49,7 +50,7 @@ class Renderer(object):
def __init__(self, file_encoding=None, string_encoding=None,
decode_errors=None, search_dirs=None, file_extension=None,
- escape=None, partials=None):
+ escape=None, partials=None, missing_tags=None):
"""
Construct an instance.
@@ -104,6 +105,11 @@ class Renderer(object):
argument to the built-in function unicode(). Defaults to the
package default.
+ missing_tags: a string specifying how to handle missing tags.
+ If 'strict', an error is raised on a missing tag. If 'ignore',
+ the value of the tag is the empty string. Defaults to the
+ package default.
+
"""
if decode_errors is None:
decode_errors = defaults.DECODE_ERRORS
@@ -117,6 +123,9 @@ class Renderer(object):
if file_extension is None:
file_extension = defaults.TEMPLATE_EXTENSION
+ if missing_tags is None:
+ missing_tags = defaults.MISSING_TAGS
+
if search_dirs is None:
search_dirs = defaults.SEARCH_DIRS
@@ -131,6 +140,7 @@ class Renderer(object):
self.escape = escape
self.file_encoding = file_encoding
self.file_extension = file_extension
+ self.missing_tags = missing_tags
self.partials = partials
self.search_dirs = search_dirs
self.string_encoding = string_encoding
@@ -222,7 +232,7 @@ class Renderer(object):
return load_template
- # TODO: rename this to _make_resolve_partial().
+ # TODO: simplify this method.
def _make_resolve_partial(self):
"""
Return the resolve_partial function to pass to RenderEngine.__init__().
@@ -230,6 +240,11 @@ class Renderer(object):
"""
if self.partials is None:
load_template = self._make_load_template()
+
+ if self.missing_tags == MissingTags.strict:
+ return load_template
+ # Otherwise, ignore missing tags.
+
def resolve_partial(name):
try:
return load_template(name)
@@ -243,14 +258,18 @@ class Renderer(object):
# a nicer exception, etc).
partials = self.partials
+ if self.missing_tags == MissingTags.strict:
+ def on_template_none(name, partials):
+ raise TemplateNotFoundError("Name %s not found in partials: %s" %
+ (repr(name), type(partials)))
+ else:
+ # Otherwise, ignore missing tags.
+ on_template_none = lambda name, partials: u''
+
def resolve_partial(name):
template = partials.get(name)
if template is None:
- return u''
-
-# if template is None:
-# raise TemplateNotFoundError("Name %s not found in partials: %s" %
-# (repr(name), type(partials)))
+ return on_template_none(name, partials)
# RenderEngine requires that the return value be unicode.
return self._to_unicode_hard(template)