[Zope] DTML Doc property from DTML Method in tag
Amos Latteier
amos@aracnet.com
Mon, 15 Feb 1999 13:30:51 -0800
At 01:20 PM 2/15/99 -0600, rkw@dataplex.net wrote:
>this is my structure:
>
>Folder 'HERE' has Folders within it.
>Assume that they have ids ID1, ID2, ID3, etc.
>Each of these Folders has a property 'common_name'
>
>Thus, in HERE/ID1 can reference <!--#var common_name--> and get 'Name 1'.
>
>In HERE, I execute a form. The checkboxes build a list ids ['ID1', 'ID2'].
>Processing the form,
><!--#var ids--> gives me the list.
><!--#in ids--><!--#var sequence-item--><!--#/in--> gives 'ID1' on the
>first iteration. But I want to get 'Name 1'
Indirection is bit confusing in DTML. Here are examples from a post I made
a couple days ago which tries to explain the concept:
Suppose that a DTML variable named Category is 'Expensive'. Then <!--#var
Category--> is 'Expensive'. Then <!--#var expr="_[Category]"--> is <!--#var
expr="_['Expensive']"--> which means <!--#var Expensive-->. Got it?
Here's one final example in Python
from DocumentTemplate import HTML
t=HTML("""<!--#var expr="_['foo']"-->, <!--#var expr="_[foo]"-->""")
print t(foo="bar", bar="spam")
which prints "bar, spam".
This is analogous to Python.
foo.bar is the same things as getattr(foo,'bar')
<!--#var bar--> is the same thing as <!--#var "_['bar']"-->
In fact, you might think of the DTML underscore namespace mapping as
similar to a Python __dict__ namespace mapping.
So to get back to your question,
><!--#var ids--> gives me the list.
><!--#in ids--><!--#var sequence-item--><!--#/in--> gives 'ID1' on the
>first iteration. But I want to get 'Name 1'
You have a list of object ids. You want to iterate through the objects and
print each one's common_name, correct?
Here's one way to do that:
<!--#in ids-->
<!--#var "_[_['sequence-item']].common_name"-->
<!--#/in-->
This assumes that common_name is a string attribute of each object. If
common_name were a method, or a DTML method you might want to do something
different, for example:
<!--#in ids-->
<!--#with "_[_['sequence-item']]"-->
<!--#var common_name-->
<!--#/with-->
<!--#/in-->
This would allow normal DTML var rendering behavior.
So why the two levels of indirection? Let's see. Assume ids is
['ID1','ID2']. The first time through the loop
<!--#var sequence-item--> == <!--#var "_['sequence-item']"--> == 'ID1'
What you are looking for is the object named ID1, not the string 'ID1'. So
you have to look in the DTML namespace for the object.
<!--#var "_['ID1']"--> == <!--#var "_[_['sequence-item']]"-->
It's a little ugly but it works.
Make sense?
-Amos