[Zope] Indirect variable references

Amos Latteier amos@aracnet.com
Thu, 04 Feb 1999 01:05:11 -0800


At 03:09 PM 2/3/99 -0800, Timothy Grant wrote:

>DTML works beautifully to allow me to build the page. The code is as follows:
>
>      <!--#in PriceCategories-->
>        <tr>
>          <td>
>            <B><!--#var Category--></B>
>          </td>
>          <td>
>            <select name="<!--#var Category-->" size=1>
>              <!--#in "PriceTable(PriceGroup=_['Category'])"-->
>                <option><!--#var Description-->
>                <!--#call "REQUEST.set('cost',Cost)"-->
>              <!--#/in-->
>            </select>
>          </td>
>          <td>
>          </td>
>        </tr>
>      <!--#/in-->  

This DTML confuses me a bit. You are building a number of select element,
one for each item in 'PriceCategories'. Each select box has options drawn
from a list of objects, one for each items in 'PriceTable'. But why is
PriceTable called with 'PriceGroup=_['Category']'? In general "_['foo']" is
the same thing as "foo", so why the indirection here? Anyway, so then you
use the Description of the things you get out of PriceTable for the option.
Fine, but why set a variable named 'cost' to Cost. This variable doesn't
seem to be used in your DTML, and besides is overwritten on each iteration
through the items from PriceTable...So in summary, it looks to me like you
can simplify:

<!--#in "PriceTable(PriceGroup=Category)"-->
<option><!--#var Description-->
<!--#/in-->

Anyway. It looks like you are making thing more complex than they need to
be. But perhaps I have misunderstood.

>So after submission, Fred, Ethel, Ricky and Lucy all contain the value of
>what was selected. How do I programatically or should that be DTMLmatically
>access those values from within another #in construct?

OK, so when you submit the form the REQUEST will have a number of variables
defined which correspond to the names of the select elements. How can you
know what those names are? Simple, just use the same objects you used to
build the form originally. The form was built by iterating over the items
in PriceCategories and creating a select element named for the Category
attribute (or method) or each item. So here's how you can find the values
in a Document which processes the submitted form:

<!--#in PriceCategories-->
the form element named <!--#var Category-->
has a value of <!--#var expr="_[Category]"-->
<!--#/in-->

Make sense? Indirection always can be confusing, and DTML's syntax doesn't
make things much easier. Let's take an example, just to make sure things
are clear.

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".

Hope this helps.

-Amos

P.S. I believe that the DTML namespace object named underscore can be used
either via the mapping protocol, or with its getitem method. In other
words, _['foo'] can also be written as _.getitem('foo') DTML looks more and
more like Perl to me all the time ;-)