[ZPT] Recursive ZPT?

Steve Spicklemire steve@spvi.com
Wed, 21 Nov 2001 06:15:41 -0500


Hi ZPTers...

I'm wondering if anyone has successfully built a recursive ZPT. I have a 
data structure that is "tree like" and I think it makes sense to render 
it in a tabular form as a recursive call to a template. The idea is that 
the template would render one level of the hierarchy and call itself to 
render lower levels. I've got it working with DTML like so:

<table border="1">
<dtml-in "currViewList">
<dtml-let   theDict=sequence-item
             theId="theDict['id']"
             name="theDict['name']"
             children="theDict.get('children',_.None)">

  <tr>
    <td><table>
        <tr><td><input name="ids:list" type="checkbox" value="&dtml-
theId;"></td>
         <td><a href="&dtml-theId;">&dtml-name;</a><br>
         (&dtml-theId;)</td></tr></table>
    </td>
  <dtml-if children>
  <td><dtml-var "tableView(_.None, _, currViewList=children)"></td>
  <dtml-else>
  </dtml-if>
  </tr>
  </dtml-let>
  </dtml-in>
  </table>

Where "currViewList" is a list of dictionaries. The display is pretty 
crude at this point.. but at least it proves the concept is workable in 
DTML. I haven't (yet!) got my head wrapped around a way to do this in 
ZPT. It seems that I should have a tal:condition=".... " that turns off 
the recursion if there are no children, but it also seems to me that 
when I include the macro for the current template I could easily get a 
recursion error but I thought.. what the heck! So I attempted to create 
a simple "test" ZPT page:

<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).

Anyway.. does anybody have any ideas of a *better* way to do this with 
ZPT?

thanks!
-steve