Indirect variable references
Hi, I'm going to try this one again with--hopefully--a little more clarity on my part. I am working on a Zope application that builds a page based on the contents of two Tiny Tables The first table provides categories, and the second provides detail for each category. 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--> As you can see the code builds a number of select boxes that are named with the names that are contained in the first Tiny Table. After the form has been submitted, I need a way of accessing the contents of those select variable names, and I cannot figure out how to do it. e.g., if the Tiny Table contains Fred, Ethel, Ricky and Lucy, I will get four select boxes named Fred, Ethel, Ricky and Lucy. It works fabulously, even though I knew it should work, I was still pumped when I looked at the HTML and saw what a nice job had been done. 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? Thanks for any assistance. -- Stand Fast, tjg. =================================== Timothy J. Grant tjg@avalongroup.net Avalon Technology Group www.avalongroup.net (503) 246-3630 voice (503) 246-3124 fax This message may be digitally signed with PGP. A PGP signature guarantees that this message really did come from me. For more information regarding digital signatures and encryption, please contact me.
On Wed, 3 Feb 1999, Timothy Grant wrote:
<!--#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-->
As you can see the code builds a number of select boxes that are named with the names that are contained in the first Tiny Table.
After the form has been submitted, I need a way of accessing the contents of those select variable names, and I cannot figure out how to do it.
Try the following <!--# in PriceCategories --> Category: <!--# var Category--> has contents: <!--# var "_.getitem(_['Category'])"--> <!--# /in--> Untested though. Pavlos
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 ;-)
Amos Latteier wrote: <a nice dissertation on variable indirection, which I now understand> I'm piggy backing this on Amos's post from a while ago, because that is where I finally found my answer. I could have sworn I had seen (and used!) a DTML code fragment that displayed the form variables sent to it, with there values. Obviously, this involves indirection. After hunting through my (extensive) zope-list archive, I couldn't find it, so I started over, with the help of Amos's text and wrote one. I often need little tools like this to debug between my forms and sql actions, so I can see what's really going on! I call it 'show_vars' The only tricky thing in it is that sequence-item is an invalid variable name, so I need to wrap it in _[''] which leads to the double wrap when dereferencing to get the value. Anyway, hope someone else finds it useful! <!--#var standard_html_header--> <table border=2> <th>Form Variable</th><th>Value</th> <!--#in "REQUEST.form.keys()"--> <tr><td><!--#var sequence-item--></td> <td><!--#var "_[_['sequence-item']]"--></td></tr> <!--#/in--> </table> <!--#var standard_html_footer--> Ross -- Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu> NSBRI Research Scientist/Programmer Computer and Information Technology Institute Rice University, 6100 S. Main St., Houston, TX 77005
participants (4)
-
Amos Latteier -
Pavlos Christoforou -
Ross J. Reedstrom -
Timothy Grant