[ZPT] Recursive ZPT?

Dieter Maurer dieter@handshake.de
Wed, 21 Nov 2001 23:53:57 +0100


Steve Spicklemire writes:
 > I'm wondering if anyone has successfully built a recursive ZPT....
 > <html>
 >    <body tal:define="global level python:3">
 >    <span metal:define-macro="foo">
 >     Level:<b tal:content="level">-1</b></span>
 >      <span tal:replace="nothing"
 >            tal:define="global level python:level-1" />
 >      <span tal:condition="python:level>0" metal:use-
 > macro="here/foo.html/macros/foo"></span>
 >    </body>
 > </html>
 > 
 > 
 > But when I save this method, ZPT replaces it with:
 > 
 > <html>
 >    <body tal:define="global level python:3">
 >    <span metal:define-macro="foo">
 >     Level:<b tal:content="level">-1</b></span>
 >      <span tal:replace="nothing"
 >            tal:define="global level python:level-1" />
 >      <span metal:use-macro="here/foo.html/macros/foo">
 >     Level:<b tal:content="level">-1</b></span>
 >    </body>
 > </html>
 > 
 > Which is probably good.. since it didn't run-away with recursion, but it 
 > also eliminated my "tal:condition" and, naturally, it doesn't do what I 
 > had hoped.. (count down to 3).
Macros are definitely not good to build recursion.

   When you read the "metal" documentation carefully you will find
   that metal attribute processing is independent of any "tal" attributes.
   This implies: you cannot control macro expansion with "tal:condition".

You should be able to recursively call a template:

    <XXX tal:replace="structure python:template(level= level-1)" />

The "level" parameter will be available inside the called ZPT as
a key in the "options" variable (a dict).


Dieter