Commit b001ae77 by Chris Jerdonek

Shared code between RenderEngine's _make_get_section() and _get_string_value().

parent f44c1e88
...@@ -93,13 +93,7 @@ class RenderEngine(object): ...@@ -93,13 +93,7 @@ class RenderEngine(object):
# and invoked as such. The returned value MUST be # and invoked as such. The returned value MUST be
# rendered against the default delimiters, then # rendered against the default delimiters, then
# interpolated in place of the lambda. # interpolated in place of the lambda.
template = val() val = self._render_value(val(), context)
if not isinstance(template, basestring):
# In case the template is an integer, for example.
template = str(template)
if type(template) is not unicode:
template = self.literal(template)
val = self._render(template, context)
if not isinstance(val, basestring): if not isinstance(val, basestring):
val = str(val) val = str(val)
...@@ -202,8 +196,8 @@ class RenderEngine(object): ...@@ -202,8 +196,8 @@ class RenderEngine(object):
# Otherwise, treat the value as a list. # Otherwise, treat the value as a list.
parts = [] parts = []
for element in data: for val in data:
if callable(element): if callable(val):
# Lambdas special case section rendering and bypass pushing # Lambdas special case section rendering and bypass pushing
# the data value onto the context stack. From the spec-- # the data value onto the context stack. From the spec--
# #
...@@ -219,14 +213,12 @@ class RenderEngine(object): ...@@ -219,14 +213,12 @@ class RenderEngine(object):
# https://github.com/defunkt/pystache/issues/113 # https://github.com/defunkt/pystache/issues/113
# #
# TODO: should we check the arity? # TODO: should we check the arity?
new_template = element(template[section_start_index:section_end_index]) val = val(template[section_start_index:section_end_index])
# Make sure we are dealing with a unicode template string. val = self._render_value(val, context, delimiters=delims)
new_template = self.literal(new_template) parts.append(val)
rendered = self._render(new_template, context, delimiters=delims)
parts.append(rendered)
continue continue
context.push(element) context.push(val)
parts.append(parsed_template.render(context)) parts.append(parsed_template.render(context))
context.pop() context.pop()
...@@ -248,6 +240,18 @@ class RenderEngine(object): ...@@ -248,6 +240,18 @@ class RenderEngine(object):
return parser.parse(template=template) return parser.parse(template=template)
def _render_value(self, val, context, delimiters=None):
"""
Render an arbitrary value.
"""
if not isinstance(val, basestring):
# In case the template is an integer, for example.
val = str(val)
if type(val) is not unicode:
val = self.literal(val)
return self._render(val, context, delimiters)
def _render(self, template, context, delimiters=None): def _render(self, template, context, delimiters=None):
""" """
Returns: a string of type unicode. Returns: a string of type unicode.
......
...@@ -11,6 +11,7 @@ import unittest ...@@ -11,6 +11,7 @@ import unittest
from pystache.context import ContextStack, KeyNotFoundError from pystache.context import ContextStack, KeyNotFoundError
from pystache import defaults from pystache import defaults
from pystache.parser import ParsingError from pystache.parser import ParsingError
from pystache.renderer import Renderer
from pystache.renderengine import context_get, RenderEngine from pystache.renderengine import context_get, RenderEngine
from pystache.tests.common import AssertStringMixin, AssertExceptionMixin, Attachable from pystache.tests.common import AssertStringMixin, AssertExceptionMixin, Attachable
...@@ -77,11 +78,9 @@ class RenderTests(unittest.TestCase, AssertStringMixin, AssertExceptionMixin): ...@@ -77,11 +78,9 @@ class RenderTests(unittest.TestCase, AssertStringMixin, AssertExceptionMixin):
Create and return a default RenderEngine for testing. Create and return a default RenderEngine for testing.
""" """
escape = defaults.TAG_ESCAPE renderer = Renderer(string_encoding='utf-8', missing_tags='strict')
engine = renderer._make_render_engine()
engine = RenderEngine(literal=unicode, escape=escape,
resolve_context=context_get,
resolve_partial=None)
return engine return engine
def _assert_render(self, expected, template, *context, **kwargs): def _assert_render(self, expected, template, *context, **kwargs):
...@@ -490,6 +489,15 @@ class RenderTests(unittest.TestCase, AssertStringMixin, AssertExceptionMixin): ...@@ -490,6 +489,15 @@ class RenderTests(unittest.TestCase, AssertStringMixin, AssertExceptionMixin):
context = {'lambda': lambda text: u'abcdé'.encode('utf-8')} context = {'lambda': lambda text: u'abcdé'.encode('utf-8')}
self._assert_render(u'abcdé', template, context, engine=engine) self._assert_render(u'abcdé', template, context, engine=engine)
def test_section__lambda__returning_nonstring(self):
"""
Test a lambda section value returning a non-string.
"""
template = '{{#lambda}}foo{{/lambda}}'
context = {'lambda': lambda text: len(text)}
self._assert_render(u'3', template, context)
def test_section__iterable(self): def test_section__iterable(self):
""" """
Check that objects supporting iteration (aside from dicts) behave like lists. Check that objects supporting iteration (aside from dicts) behave like lists.
......
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