Commit 21ab97cd by Chris Jerdonek

Fixed issue #113: "Section lambdas mistakenly pushed onto context stack"

parent 2adac861
......@@ -4,7 +4,8 @@ History
0.6.0 (TBD)
-----------
* Falsey values now coerced to strings using str().
* Bugfix: falsey values now coerced to strings using str().
* Bugfix: section-lambda return values no longer pushed onto context stack.
0.5.1 (2012-04-24)
------------------
......
......@@ -161,7 +161,12 @@ class RenderEngine(object):
# TODO: should we check the arity?
template = data(template)
parsed_template = self._parse(template, delimiters=delims)
data = [data]
# Lambdas special case section rendering and bypass pushing
# the data value onto the context stack. Also see--
#
# https://github.com/defunkt/pystache/issues/113
#
return parsed_template.render(context)
else:
# The cleanest, least brittle way of determining whether
# something supports iteration is by trying to call iter() on it:
......
......@@ -501,6 +501,40 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
context = {'person': 'Mom', 'test': (lambda text: text + " :)")}
self._assert_render(u'Hi Mom :)', template, context)
def test_section__lambda__not_on_context_stack(self):
"""
Check that section lambdas are not pushed onto the context stack.
Even though the sections spec says that section data values should be
pushed onto the context stack prior to rendering, this does not apply
to lambdas. Lambdas obey their own special case.
This test case is equivalent to a test submitted to the Mustache spec here:
https://github.com/mustache/spec/pull/47 .
"""
context = {'foo': 'bar', 'lambda': (lambda text: "{{.}}")}
template = '{{#foo}}{{#lambda}}blah{{/lambda}}{{/foo}}'
self._assert_render(u'bar', template, context)
def test_section__lambda__no_reinterpolation(self):
"""
Check that section lambda return values are not re-interpolated.
This test is a sanity check that the rendered lambda return value
is not re-interpolated as could be construed by reading the
section part of the Mustache spec.
This test case is equivalent to a test submitted to the Mustache spec here:
https://github.com/mustache/spec/pull/47 .
"""
template = '{{#planet}}{{#lambda}}dot{{/lambda}}{{/planet}}'
context = {'planet': 'Earth', 'dot': '~{{.}}~', 'lambda': (lambda text: "#{{%s}}#" % text)}
self._assert_render(u'#~{{.}}~#', template, context)
def test_comment__multiline(self):
"""
Check that multiline comments are permitted.
......
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