[Zope] Getting a document's contents
R. David Murray
bitz@bitdance.com
Tue, 15 Aug 2000 13:26:56 -0400 (EDT)
On Tue, 15 Aug 2000, Joshua Brauer wrote:
> I have a folder (Parts) that contains dtml documents that are snippets of code. I want to include some of them which I have listed in a variable (select).
>
> So I use code like this:
>
> <dtml-with parts>
> <dtml-in select>
> <dtml-var "_.getitem('sequence-item')">
> </dtml-in>
> </dtml-with>
Right idea, but not quite there. Your 'select' variable holds the
string ids of the objects, but you need the actual objects in order
to get them rendered. getitem will look up the object given the
id, but you're actually telling it here to look up the value (in
the namespace) of the string 'sequence-item'. What you get, as
you observed, is the id string stored at that moment in sequence-item.
So you need to loose the quotes so it will look up that id string
in the namespace. However, there's a wrinkle: sequence-item
is not a valid python variable name. So you have to look sequence-item
up in the namespace after all. You could do:
<dtml-var "_.getitem(_.getitem('sequence-item'))">
However, Zope provides a shortcut for this, using the namespace variable
(_) as a dictionary:
<dtml-var "_.getitem(_['sequence-item'])">
This, though, is going to *just* retrieve the object, it won't cause
it to render (I think you'll get the DTML source, which I think is
the representation of the object as string, which is the default
conversion in this context <grin>)>.
So what you a really want to do is execute the object after you've
retreived it, which will case it (unless it is a ZClass that is
not subclassed from Renderable) to render itself, which is what
I think you want. To do that you pass a true flag to getitem as
its second argument:
<dtml-var "_.getitem(_['sequence-itmem'],1)">
Now, _ has this additional feature (Chris Whithers would say bug) that it
calls any callable object it is asked to retrieve via the dictionary lookup.
So you should be able to shorten this to:
<dtml-var "_[_['sequence-item]]">
There's one more subtlety (are you getting tired of this yet?) involving
the need to pass the namespace to called DTML methods, but
I believe _ takes care of that for you in this example so I won't
go into it. But it's a FAQ, too, so you might want to look for
the answer before it bites you <grin>.
--RDM