Forums / Developer / The select="selected" problem
Esben Maaløe
Sunday 22 June 2003 7:15:17 am
I just made something that needed a select box. The select box should be able to default to a certain value. I had to do it this way:
<option value="Afghanistan" {switch match=$selected_option}{case match='Afghanistan'}selected="selected"{/case}{/switch}>Afghanistan</option> <option value="Albania" {switch match=$selected_option}{case match='Albania'}selected="selected"{/case}{/switch}>Albania</option><option value="Algeria" {switch match=$selected_option}{case match='Algeria'}selected="selected"{/case}{/switch}>Algeria</option>
In my case that gives more than 200 switch statements that I have to execute.
I'd much prefer something like
{ let $$selected_option='selected="selected"' } <option value="Afghanistan" {$Afghanistan}>Afghanistan</option> <option value="Albania" {$Albania}>Albania</option> <option value="Algeria" {$Algeria}>Algeria</option>{/let}
Where $$selected_option works just like in PHP - a variable variablename.
Will the template language support variable variablenames in the future?
How could I have made my select box thing smarter ?
Monday 23 June 2003 10:52:26 am
Come on - it's an interesting question :)
Björn X
Monday 23 June 2003 9:04:09 pm
Just to give you a rough idea
{let countries=array('Germany','_Norway') selected='Germany' } .... {section loop=$countries} {switch} <option value="{$:item}" {case match=eq($:item,$:selected)} selected {case}{/case} {/swtich} >{$:item}</option>{/section}
Tuesday 24 June 2003 5:55:09 am
Thanks Björn - that is certainly more elegant.