changeset 51:a572b1018b66

Fix `py:for` directive when combined with other directives (such as `py:strip`).
author cmlenz
date Tue, 04 Jul 2006 09:03:04 +0000
parents a053ffb834cb
children 584dff20e91f
files markup/template.py markup/tests/template.py
diffstat 2 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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()
 
--- a/markup/tests/template.py
+++ b/markup/tests/template.py
@@ -55,6 +55,28 @@
         </doc>""", 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("""<doc xmlns:py="http://purl.org/kid/ns#">
+          <div py:for="item in items" py:strip="">
+            <b>${item}</b>
+          </div>
+        </doc>""")
+        self.assertEqual("""<doc>
+            <b>1</b>
+            <b>2</b>
+            <b>3</b>
+            <b>4</b>
+            <b>5</b>
+        </doc>""", 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
Copyright (C) 2012-2017 Edgewall Software