[Zope] calling methods for each object in a list

Rik Hoekstra rik.hoekstra@inghist.nl
Thu, 30 Mar 2000 22:46:34 +0200


>Philipp Dunkel schrieb:
>[...]
>>
>> when I do a
>>
>> <dtml-in ids>
>>  <dtml-var sequence-item>
>> </dtml-in>
>>
>> it prints out the ids of the checked objects fine,
>> but when I try a
>>
>> <dtml-in ids>
>>  <dtml-call "sequence-item.foo">
>> </dtml-in>
>>
>> he doesn't know sequence-item anymore
>
>No, Zope can't, especially Python can't, because it trys to evaluate
>something like sequence - item.foo.
>
>So try
>
><dtml-with sequence-item>
> <dtml-call "foo">
></dtml-with
>
>or:
>
><dtml-call "_.['sequence-item'].foo">
>


Hm. In the example

<dtml-in list>
  <dtml-var foo>
</dtml-in>

the iteration will change the namespace to that of each sequence-item
automatically. So the foo asked for will always be the foo of the current
sequence-item. Things like
<with sequence-item> or "_['sequence-item']..." are not necessary.

If you iteration is by a number of IDs, as your question suggests, though,
the example is not enough. When you iterate over a list of IDs, the
sequence-item will be an ID (that is a string) and foo will have no meaning
(it will throw an AttributeError).
There are two way of remediating this:

preferrably use objectValues(), like so

<dtml-in objectValues>
   <dtml-var foo>
</dtml-in>

If you _have_ to use a list of ids, things get more complicated

<dtml-in Ids>
  <dtml-var "_.getitem(_['sequence-item'].foo)">
</dtml-in>

(sorry about that)

All code is untested, but it should work.

Rik