From: Noah Morrison Date: Sat, 29 Sep 2018 05:41:24 +0000 (-0400) Subject: Fix nested loops X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=076fbfaf34e4373ec1474b6c115c74664f7380b1;p=chevron.git Fix nested loops This feels like it might still have edge cases, but it works for now. --- diff --git a/chevron/renderer.py b/chevron/renderer.py index 3c284d1..98db93d 100644 --- a/chevron/renderer.py +++ b/chevron/renderer.py @@ -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 diff --git a/test_spec.py b/test_spec.py index ccb136b..67d5ff5 100755 --- a/test_spec.py +++ b/test_spec.py @@ -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__":