]> Devi Nivas Git - chevron.git/commitdiff
Fix inverted sections coercing the . variable
authorNoah Morrison <noah@morrison.ph>
Wed, 14 Jun 2017 04:36:51 +0000 (00:36 -0400)
committerNoah Morrison <noah@morrison.ph>
Wed, 14 Jun 2017 04:36:51 +0000 (00:36 -0400)
Fixes #17

chevron/renderer.py
test_spec.py

index e01a3e4d4a8b631687d53bd4390f538fe520b7de..0a65bd0469b92731c0019a92bfae67fcac437a48 100644 (file)
@@ -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)
index 2c86e6a3215c63a6805ddf1f8c100967913a82b2..de2130fb9bd06731b249744ed2eb26de29847f4b 100755 (executable)
@@ -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__":