From 80a6898db848cf5df8111753c2e6546450a565ed Mon Sep 17 00:00:00 2001 From: Noah Morrison Date: Wed, 14 Jun 2017 00:36:51 -0400 Subject: [PATCH] Fix inverted sections coercing the . variable Fixes #17 --- chevron/renderer.py | 5 +++++ test_spec.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/chevron/renderer.py b/chevron/renderer.py index e01a3e4..0a65bd0 100644 --- a/chevron/renderer.py +++ b/chevron/renderer.py @@ -191,6 +191,11 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', elif tag == 'variable': # Add the html escaped key to the output thing = _get_key(key, scopes) + if thing is True and key == '.': + # if we've coerced into a boolean by accident + # (inverted tags do this) + # then get the un-coerced object (next in the stack) + thing = scopes[1] if type(thing) != unicode: thing = unicode(str(thing), 'utf-8') output += _html_escape(thing) diff --git a/test_spec.py b/test_spec.py index 2c86e6a..de2130f 100755 --- a/test_spec.py +++ b/test_spec.py @@ -177,6 +177,20 @@ class ExpandedCoverage(unittest.TestCase): self.assertEqual(result, expected) + # https://github.com/noahmorrison/chevron/issues/17 + def test_inverted_coercion(self): + args = { + 'template': '{{#object}}{{^child}}{{.}}{{/child}}{{/object}}', + 'data': {'object': [ + 'foo', 'bar', {'child': True}, 'baz' + ]} + } + + result = chevron.render(**args) + expected = 'foobarbaz' + + self.assertEqual(result, expected) + # Run unit tests from command line if __name__ == "__main__": -- 2.47.3