[ZPT] Re: Recursive structures

Tonico Strasser contact_tonico at yahoo.de
Fri Jul 1 19:28:42 EDT 2005


(Reposting to the right mailing list).

Ian Bicking schrieb:
> I'm surprised this has never come up for me before, but now I want to 
> render a recursive data structure and I'm at a loss how I might do that 
> in ZPT.  Or, what the best workaround would be.  E.g.:
> 
> ['a', 'b', ['c', ['d', 'e']]]
> 
> Becomes:
> 
> <ul>
>  <li>a</li>
>  <li>b</li>
>  <ul>
>   <li>c</li>
>   <ul>
>    <li>d</li>
>    <li>e</li>
>   </ul>
>  </ul>
> </ul>
> 

The code above is not valid HTML[1]. This is one way how to do it:

<ul tal:define="nodes python:['a', 'b', ['c', ['d', 'e']]]">
   <li metal:define-macro="item"
       tal:repeat="node nodes">
     <span tal:define="global is_list python:same_type([], node);
                       content python:is_list and 'list:' or node"
           tal:replace="content"/>
     <ul tal:condition="is_list"
         tal:define="nodes node">
       <li metal:use-macro="template/macros/item"/>
     </ul>
   </li>
</ul>

> I guess the template could call itself repeatedly.  Which means the list 
> can't be embedded in any other markup.

To embed it in another template simply call it:

<ul tal:replace="structure here/list"/>

(Or define a macro and use that).

Even better if the logic is in a script that renders the template, I think.

Tonico

[1] <ul> can not have <ul> as parent
http://zvon.org/xxl/xhtmlReference/Output/Strict/el_ul.html



More information about the ZPT mailing list