Commit bc2ca04c by Chris Jerdonek

Added to Renderer class support for missing_tags (not finished yet).

Support for partial loading added.  Still need to support missing tags.
parent b6c4b4af
......@@ -26,6 +26,14 @@ def read(path):
f.close()
class MissingTags(object):
"""Contains the valid values for Renderer.missing_tags."""
ignore = 'ignore'
strict = 'strict'
class PystacheError(Exception):
"""Base class for Pystache exceptions."""
pass
......
......@@ -17,6 +17,8 @@ except ImportError:
import os
import sys
from pystache.common import MissingTags
# How to handle encoding errors when decoding strings from str to unicode.
#
......@@ -36,6 +38,9 @@ STRING_ENCODING = sys.getdefaultencoding()
# strings that arise from files.
FILE_ENCODING = sys.getdefaultencoding()
# How to handle missing tags when rendering a template.
MISSING_TAGS = MissingTags.ignore
# The starting list of directories in which to search for templates when
# loading a template by file name.
SEARCH_DIRS = [os.curdir] # i.e. ['.']
......
......@@ -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)
......
......@@ -124,6 +124,22 @@ class RendererInitTestCase(unittest.TestCase):
renderer = Renderer(file_extension='foo')
self.assertEqual(renderer.file_extension, 'foo')
def test_missing_tags(self):
"""
Check that the missing_tags attribute is set correctly.
"""
renderer = Renderer(missing_tags='foo')
self.assertEqual(renderer.missing_tags, 'foo')
def test_missing_tags__default(self):
"""
Check the missing_tags default.
"""
renderer = Renderer()
self.assertEqual(renderer.missing_tags, 'ignore')
def test_search_dirs__default(self):
"""
Check the search_dirs default.
......@@ -457,19 +473,19 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase, AssertStringMixin, Asser
self.assertString(resolve_partial('foo'), u'')
# TODO: add this test case back when we add support to Renderer for strict mode.
# def test__resolve_partial__not_found__default(self):
# """
# Check that resolve_partial provides a nice message when a template is not found.
def test__resolve_partial__not_found__strict__default(self):
"""
Check that resolve_partial provides a nice message when a template is not found.
# """
# renderer = Renderer()
"""
renderer = Renderer()
renderer.missing_tags = 'strict'
# engine = renderer._make_render_engine()
# resolve_partial = engine.resolve_partial
engine = renderer._make_render_engine()
resolve_partial = engine.resolve_partial
# self.assertException(TemplateNotFoundError, "File 'foo.mustache' not found in dirs: ['.']",
# resolve_partial, "foo")
self.assertException(TemplateNotFoundError, "File 'foo.mustache' not found in dirs: ['.']",
resolve_partial, "foo")
def test__resolve_partial__not_found__dict(self):
"""
......@@ -484,22 +500,22 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase, AssertStringMixin, Asser
self.assertString(resolve_partial('foo'), u'')
# TODO: add this test case back when we add support to Renderer for strict mode.
# def test__resolve_partial__not_found__dict(self):
# """
# Check that resolve_partial provides a nice message when a template is not found.
def test__resolve_partial__not_found__strict__dict(self):
"""
Check that resolve_partial provides a nice message when a template is not found.
# """
# renderer = Renderer()
# renderer.partials = {}
"""
renderer = Renderer()
renderer.missing_tags = 'strict'
renderer.partials = {}
# engine = renderer._make_render_engine()
# resolve_partial = engine.resolve_partial
engine = renderer._make_render_engine()
resolve_partial = engine.resolve_partial
# Include dict directly since str(dict) is different in Python 2 and 3:
# <type 'dict'> versus <class 'dict'>, respectively.
# self.assertException(TemplateNotFoundError, "Name 'foo' not found in partials: %s" % dict,
# resolve_partial, "foo")
# Include dict directly since str(dict) is different in Python 2 and 3:
# <type 'dict'> versus <class 'dict'>, respectively.
self.assertException(TemplateNotFoundError, "Name 'foo' not found in partials: %s" % dict,
resolve_partial, "foo")
## Test the engine's literal attribute.
......
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