[Zope3-Users] Re: Traversal Tricks (was Seeking Reference Guide on TAL/TALES/METAL)

Jeff Rush jeff at taupro.com
Sat Apr 1 07:34:07 EST 2006


Jeff Rush wrote:
> 
> My specific question is how evaluation of the path expression "X/Z" is 
> handled.  It *seems* to be:
> 
>     X.__getitem__('Y')    # X['Y']
> 
> whereas I'm trying to get:
> 
>     X.__getattr__('Y')    # X.Y
> 
> Under Zope 2, as I recall, it would try several algorithms; attribute, 
> mapping, sequence.  And I'm wondering if that mechanism was simplified 
> for Zope 3, and in what way.

My approach of using attr: and item: TALES namespace adapters doesn't work 
in all cases, since those TAL templates not under my control, for ZMI, 
access the attribute/item namespaces using the old ordering.

So another approach is to override the traversal algorithm for selected 
classes of objects.

There are (at least) two traversal algorithms, one that seaches the item 
namespace first then the attribute namespace, and one that does it in the 
other order.

   zope.app.traversing.adapters.DefaultTraversable  (attr then item)

   zope.app.container.traversal.ContainerTraversable (item then attr)

Therefore I didn't need to write my own traverser, just select the 
non-content style for those containers that needs the other order.

So for my IVenture container class, I do:

   <zope:adapter
       factory="zope.app.traversing.adapters.DefaultTraversable"
 
 

       provides="zope.app.traversing.interfaces.ITraversable"
       for=".interfaces.IVenture"
       />
 
 

And the old search order is now 'attr then item'!

I thought this technique might useful to someone.

-Jeff



More information about the Zope3-users mailing list