[Zope] Indirection, acquisition, asphyxiation
Steve Alexander
steve@cat-box.net
Fri, 15 Sep 2000 08:59:37 +0100
Dennis Nichols wrote:
> I'm having trouble using the value of a property to determine which
> object to render (Zope 2.2.1, if it makes any difference). Starting from
> the beginning, let's say I have two folders A and B with four DTML
> methods (Nilo, Alpha, Smalpha, and Bravo):
>
> /
> Nilo
> /A
> Alpha
> Smalpha
> /B
> Bravo
>
> From Alpha, I can render Nilo, Smalpha, and Bravo by using this dtml:
>
> <dtml-var Nilo> (or <dtml-var "Nilo">)
> <dtml-var Smalpha> (or <dtml-var "Smalpha">)
> <dtml-var "B.Bravo"> (not <dtml-var B.Bravo>!)
>
> OK, now I want a property of folder A, call it 'choice', to determine
> what should be rendered by Alpha. So I rewrite Alpha to include:
>
> either <dtml-var "_[choice]">
> or <dtml-var "_.getitem(choice)">
>
> These work nicely if I set 'choice' to 'Nilo' or 'Smalpha' but I don't
> see how to make either one work for Bravo! Setting 'choice' to 'B.Bravo'
> gives me:
>
> Error Type: KeyError
> Error Value: B.Bravo
>
> How do I get this to work for Bravo (and still keep it working for the
> other cases)? Clearly my zen needs some tending to.
You can use restrictedTraverse('slash/delimited/path')
If you have a slash at the start, the traversal starts from the Zope
root object. Otherwise, it is relative.
You can also use a sequence to represent your path. The slash at the
start is represented by an empty string at the beginning of your sequence.
See the documentation on the interfaces Wiki.
http://www.zope.org/Members/michel/Projects/Interfaces/Traversal
Also, see lib/python/OFS/Traversable.py
--
Steve Alexander
Software Engineer
Cat-Box limited
http://www.cat-box.net