]> Devi Nivas Git - chevron.git/commitdiff
Fix nested loops
authorNoah Morrison <noah@morrison.ph>
Sat, 29 Sep 2018 05:41:24 +0000 (01:41 -0400)
committerNoah Morrison <noah@morrison.ph>
Sat, 29 Sep 2018 05:41:24 +0000 (01:41 -0400)
This feels like it might still have edge cases, but it works for now.

chevron/renderer.py
test_spec.py

index 3c284d116aa8ce69ebf207c50fbd5ca21dc683d8..98db93d4cac427a6fb2f41234a41bd555be4f3f6 100644 (file)
@@ -273,10 +273,16 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
                 # Then we need to do some looping
 
                 # Gather up all the tags inside the section
+                # (And don't be tricked by nested end tags with the same key)
+                # TODO: This feels like it still has edge cases, no?
                 tags = []
+                tags_with_same_key = 0
                 for tag in tokens:
+                    if tag == ('section', key):
+                        tags_with_same_key += 1
                     if tag == ('end', key):
-                        break
+                        tags_with_same_key -= 1
+                        if tags_with_same_key < 0: break
                     tags.append(tag)
 
                 # For every item in the scope
index ccb136b1c32c67e2496b2c5f6c9e054f3456bb0b..67d5ff5d7bc0621f11d0d1b31e1503154cae4923 100755 (executable)
@@ -310,6 +310,18 @@ class ExpandedCoverage(unittest.TestCase):
 
         self.assertEqual(result, expected)
 
+    # https://github.com/noahmorrison/chevron/issues/39
+    def test_nest_loops_with_same_key(self):
+        args = {
+            'template': 'A{{#x}}B{{#x}}{{.}}{{/x}}C{{/x}}D',
+            'data': {'x': ['z', 'x']}
+        }
+
+        result = chevron.render(**args)
+        expected = 'ABzxCBzxCD'
+
+        self.assertEqual(result, expected)
+
 
 # Run unit tests from command line
 if __name__ == "__main__":