[ZPT] Recursive structures

Ian Bicking ianb at colorstudy.com
Fri Jul 1 16:42:11 EDT 2005


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>


I guess the template could call itself repeatedly.  Which means the list 
can't be embedded in any other markup.  Hrm... in Cheetah I'd do:

#def make_list(items)
<ul>
  #for item in items
   #if isinstance(item, list)
    $make_list(item)
   #else
    <li>$item</li>
   #end if
  #end for
</ul>
#end def
$make_list(items)

It's a code-heavy template, and maybe it should just be written in 
Python, but it's also reasonable to allow people to add classes to 
items, further logic, etc.

-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the ZPT mailing list