# HG changeset patch # User cmlenz # Date 1152003784 0 # Node ID a572b1018b66d1361be84e93d8a606cbad405659 # Parent a053ffb834cb3facbce09967d11c5a385f781b0f Fix `py:for` directive when combined with other directives (such as `py:strip`). diff --git a/markup/template.py b/markup/template.py --- a/markup/template.py +++ b/markup/template.py @@ -376,10 +376,10 @@ for idx, name in enumerate(self.targets): scope[name] = item[idx] ctxt.push(**scope) + output = stream if directives: - stream = list(directives[0](iter(stream), ctxt, - directives[1:])) - for event in stream: + output = directives[0](iter(output), ctxt, directives[1:]) + for event in output: yield event ctxt.pop() diff --git a/markup/tests/template.py b/markup/tests/template.py --- a/markup/tests/template.py +++ b/markup/tests/template.py @@ -55,6 +55,28 @@ """, str(tmpl.generate())) +class ForDirectiveTestCase(unittest.TestCase): + """Tests for the `py:def` template directive.""" + + def test_loop_with_strip(self): + """ + Verify that the a named template function with a strip directive + actually strips of the outer element. + """ + tmpl = Template(""" +
+ ${item} +
+
""") + self.assertEqual(""" + 1 + 2 + 3 + 4 + 5 + """, str(tmpl.generate(Context(items=range(1, 6))))) + + class MatchDirectiveTestCase(unittest.TestCase): """Tests for the `py:match` template directive.""" @@ -262,6 +284,7 @@ suite.addTest(unittest.makeSuite(TemplateTestCase, 'test')) suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test')) suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test')) + suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test')) suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test')) suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test')) return suite