Ian Bicking wrote:

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.

I think it should *definitely* be written in Python, although a "Script (Python)" should suffice. Add a "Script (Python)" with an ID of "make_list" and a parameter list of "items" The following is untested, but should work, I think. Beware of any special text in the strings (&,<,>,etc).

######
text = "<ul>\n"
for item in items:
  if isinstance( item, ( list, tuple )):
      text += context.make_list( item )
  else:
     text += "<li>" + str(item) + "</li>\n"
return text + "</ul>"
############

_______________________________________________
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )

Reply via email to