[Zope-CMF] Re: Re-working Breadcrumbs

Tres Seaver tseaver@palladion.com
Wed, 29 Aug 2001 08:38:53 -0400 (EDT)


On Wed, 29 Aug 2001 davelehman@loewen.com wrote:

> 
> >We actually have a python script for this coming to a CMFDecor cvs
> checkout
> >near you.
> >I'll finally get my cvs up to date with some fixes/improvments and other
> >general stuff today.
> >You can look at the breadcrumbs.py script in CMFDecor/skins/zpt_generic
> for
> >a working example of what you're looking for.
> 
> Alrightly, I got the script from CVS last night. It's close, but still not
> quite what i'm looking for.
> 
> The old breadcrumbs script created this:
> 
> id > id > id > id
> 
> The new breadcrumbs script displays breadcrumbs like this:
> 
> id > id > id > title
> 
> And what I want is this:
> 
> title > title > title > title
> 
> I'm not trying to be a pain, but is what i'm looking for unreasonable? I
> had an almost-working version of breadcrumbs (which displayed all titles)
> in DTML, but I thought that using a Python script would be easier....
> 
> So, I think we're almost there-- when iterating through the loop, we do
> have the URL for the folder at each level. So I guess what I really need to
> know is:
> 
> - when I have the URL of a folder, how do I get a handle for that folder
> object so I can grab the title from it?
> 
>      current_folder = getobject(url)
>      breadcrumb_title = current_folder.Title

The Right Thing (tm) for the Python script to do is to return
a datastructure which the presentation can use to construct
the appropriate links.

We want something like a list of dictionaries::

  [ { 'url' : url1, 'title' : title1, 'id' : id1 }
  , { 'url' : url2, 'title' : title2, 'id' : id2 }
  ...
  ]

So, we might do::

    ## Script (Python) "breadcrumbs.py $Revision: 1.1 $"
    ##bind context=context
    ##bind namespace=
    ##bind script=script
    ##bind subpath=traverse_subpath
    ##parameters=include_root=1
    ##title=Return breadcrumbs
    ##
    from string import join

    result = []
    portal_url = context.portal_url()

    if include_root:
        result.append( { 'id'      : 'root'
                       , 'title'   : context.portal_properties.title()
                       , 'url'     : portal_url
                       }
                     )

    relative = context.portal_url.getRelativeContentPath( context )
    portal = context.portal_url.getPortalObject()

    for i in range( len( relative ) ):
        now = relative[ :i+1 ]
        obj = portal.restrictedTraverse( now )
        result.append( { 'id'      : now[ -1 ]
                       , 'title'   : obj.Title()
                       , 'url'     : portal_url + '/' + join( now, '/' )
                       }
                     )

    return result

and then, in the template::

    <div tal:define="crumbs here/breadcrumbs"
         class="Desktop">
    <span tal:repeat="crumb crumbs">
    / <a href="url" tal:attributes="href crumb/url"
            tal:content="crumb/id"> id </a>
    </span>
    </div>

or, to get what David asked for::

    <div tal:define="crumbs here/breadcrumbs"
         class="Desktop">
    <span tal:repeat="crumb crumbs">
    / <a href="url" tal:attributes="href crumb/url"
            tal:content="crumb/title"> title </a>
    </span>
    </div>

Tres.
-- 
===============================================================
Tres Seaver                                tseaver@zope.com
Zope Corporation      "Zope Dealers"       http://www.zope.com