[Zope] Left navigation bar; how to implement one.

Tino Wildenhain tino@wildenhain.de
Sun, 15 Dec 2002 00:26:34 +0100


Hi Hans,

--On Samstag, 14. Dezember 2002 16:49 -0500 Hans Deragon <hans@deragon.biz> 
wrote:

> Greetings.
>
>
>    I searched in many places, but I could not find an example on how to
> create a left navigation bar with Zope.  I might have some ideas, but I
> would like to hear from you oldies (as opposed to newbies) the most
> efficient way to create one.  Off course, the left navigation bar should
> show up on each web page of the site, it would expand as the user goes in
> deeper on the site and you would only need to write it once and use it on
> all of the web pages.
>
>    I am a newbie to Zope, but not in html or python.
>

Simple solution:

In root or where you want your navigation start, create a pythonscript,
lets call it "navigation" like this:
---- *snip* ---
# first get all the parents from REQUEST (a list with all the objects in 
URL)
parents=context.REQUEST.PARENTS

# then a simple recursive helper function
def buildnavigation(baseobject):
    # decide if we have to look deeper (when we are in the path)
    if baseobject in parents:
        return [{'folder':obj,
                 'subfolders':buildnavigation(obj)
                } for obj in baseobject.objectValues('Folder')]
    else:
        return []

# return the list in list in list object
return buildnavigation(container)
---- *snip* ----

In ZPT, it is a bit ugly, but this way you dont violate standards,
the idea is to create all levels of navigation (you sure will have
a maximum depth you want to display)

<div metal:define-macro="navigation">
<ul>
 <li tal:repeat="nav here/navigation">
  <a href="." tal:attributes="href nav/folder/absolute_url" 
tal:content="nav/folder/title_or_id">..</a>
  <ul tal:condition="nav/subfolders">
   <li tal:repeat="nav nav/subfolders">
    <a href="." tal:attributes="href nav/folder/absolute_url" 
tal:content="nav/folder/title_or_id">..</a>
    <ul tal:condition="nav/subfolders">
     <li tal:repeat="nav nav/subfolders">
      <a href="." tal:attributes="href nav/folder/absolute_url" 
tal:content="nav/folder/title_or_id">..</a>
      <ul tal:condition="nav/subfolders">
       <li tal:repeat="nav nav/subfolders">
       <a href="." tal:attributes="href nav/folder/absolute_url" 
tal:content="nav/folder/title_or_id">..</a>
       </li>
      </ul>
     </li>
    </ul>
   </li>
 </ul>
</li>
</ul>
</div>

You can use this macro in all your pages
or define a macro for a page which includes the construct
and defines a slot for your variable content.

HTH
Tino Wildenhain