'creating' a variable from sequence-item
Hi, I've got a page where 'n' radio button sets are displayed and the use has to select a correct answer from each of the groups. Dumping the data out is easy: (questions is a lines property with a single entry 'Is the sky blue?' type question. Note the last piece to tell the form how many questions where on the screen. Answers please::<br> <dtml-in questions> <dtml-var sequence-item>:: <input type=radio name=answer<dtml-var sequence-index> value="1"> (true) <input type=radio name=answer<dtml-var sequence-index> value="-1"> (false) <input type=radio name=answer<dtml-var sequence-index> value="0" checked> (dunno) <br> <dtml-if sequence-end> <input type=hidden name=numquestions:int value=<dtml-var sequence-index>> </dtml-if> </dtml-in> Notes: 1) checked means the radio button is set, that stops problems with variables such as 'answern' not being defined. Pedagogically, it's an open call whether you should assume the student doesn't know to begin with :) 2) numquestions:int stops problems with the numquestions+1 code a bit later on. Processing them is easy *if* I don't mind what order the keys are displayed in... <dtml-in "REQUEST.form.items()"> <dtml-var sequence-key> : <dtml-var sequence-item><BR> </dtml-in> Notes: Pretty standard stuff. form is a dictionary containing all the variables in the form (ie REQUEST.form.['answer2'] will get that value). unfortunately, I do mind, so I have to do some hairy hoop jumping... <dtml-in "_.range(0, numquestions+1)"> Variable Name: answer<dtml-var sequence-index>, Composed Variable Name=<dtml-var "_.string.join(('answer', _.str(_['sequence-index'])),'')">, Value of Composed Variable Name: <dtml-var "_[_.string.join(('answer', _.str(_['sequence-index'])),'')]"><br> </dtml-in> Notes: 1) _range(0, numquestions+1) is in Zope 2 and acts like the Python range command, except it's got some error checking. 2) _.str(['sequence-index']) is needed to convert sequence-index into a string so that string.join can do its work (the delimiter is '' so it's a standard join). 3) the _[] part is to 'lookup' the Composed Variable name in the namespace. yikes! I *must* be doing something dumb here :) Any thoughts on doing it better? cheers tone. ------ Dr Tony McDonald, FMCC, Networked Learning Environments Project http://nle.ncl.ac.uk/ The Medical School, Newcastle University Tel: +44 191 222 5888 Fingerprint: 3450 876D FA41 B926 D3DD F8C3 F2D0 C3B9 8B38 18A2
On Wed, 28 Jul 1999, Tony McDonald wrote:
Notes: 1) checked means the radio button is set, that stops problems with variables such as 'answern' not being defined. Pedagogically, it's an open call whether you should assume the student doesn't know to begin with :) 2) numquestions:int stops problems with the numquestions+1 code a bit later on.
Processing them is easy *if* I don't mind what order the keys are displayed in... <dtml-in "REQUEST.form.items()"> <dtml-var sequence-key> : <dtml-var sequence-item><BR> </dtml-in> <dtml-in "range(numquestions)"> KEY: <dtml-var "'answer%d' % _['sequence-item']"> VALUE: <dtml-var "_['answer%d' % _['sequence-item']]"> </dtml-in>
yikes! I *must* be doing something dumb here :) Not really. You just encountered a thing that is seldom needed in standard Zope I'd assume.
Andreas -- Andreas Kostyrka | andreas@mtg.co.at phone: +54/1/7070750 | phone: +43/676/4091256 MTG Handelsges.m.b.H. | fax: +43/1/7065299 Raiffeisenstr. 16/9 | 2320 Zwoelfaxing AUSTRIA
<dtml-in "range(numquestions)"> KEY: <dtml-var "'answer%d' % _['sequence-item']"> VALUE: <dtml-var "_['answer%d' % _['sequence-item']]"> </dtml-in>
Cheers for this Andreas, it works really well (I had to use a %s though, as I was getting problems with integers & strings).
yikes! I *must* be doing something dumb here :) Not really. You just encountered a thing that is seldom needed in standard Zope I'd assume.
That's good news, I shall probably write an external method for it...eventually. ta tone. ------ Dr Tony McDonald, FMCC, Networked Learning Environments Project http://nle.ncl.ac.uk/ The Medical School, Newcastle University Tel: +44 191 222 5888 Fingerprint: 3450 876D FA41 B926 D3DD F8C3 F2D0 C3B9 8B38 18A2
participants (2)
-
Andreas Kostyrka -
Tony McDonald