[Zope] Re: [ZPT] Recursive structures

J Cameron Cooper zope-l at jcameroncooper.com
Fri Jul 1 20:32:21 EDT 2005


Nikko Wolf wrote:
> 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've made recursive macros before. It's not so different from other 
recursion techniques. Here's one to render a site tree::

  <html>
    <head>
      <title tal:content="template/title">The title</title>
    </head>
    <body>
      Site structure:

    <tal:contain define="location nocall:here">
     <div metal:use-macro="template/macros/list" />
    </tal:contain>

    </body>
  </html>

  <tal:hidemacro replace="nothing">
  <metal:recurse define-macro="list">
    <ul>
      <li tal:repeat="elt location/objectValues">
        <span tal:content="elt/getId">objectId</span>
        <metal:block tal:condition="elt/isPrincipiaFolderish"
                     tal:define="location nocall:elt">
            <div metal:use-macro="template/macros/list" />
        </metal:recurse>
      </li>
    </ul>
  </metal:recurse>
  </tal:hidemacro>

It will go from 'context', of course, unless you were to change the 
definition of 'location' in the page.

You can imagine how this would work with a nested list: you just have to 
have ways of checking containership and getting contents.

		--jcc

-- 
"Building Websites with Plone"
http://plonebook.packtpub.com/

Enfold Systems, LLC
http://www.enfoldsystems.com


More information about the Zope mailing list