[Zope] 'creating' a variable from sequence-item

Tony McDonald tony.mcdonald@ncl.ac.uk
Wed, 28 Jul 1999 16:36:13 +0100


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