Forums / Developer / The select="selected" problem

The select="selected" problem

Author Message

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 ?

Esben Maaløe

Monday 23 June 2003 10:52:26 am

Come on - it's an interesting question :)

Björn [email protected]

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}

Looking for a new job? http://www.xrow.com/xrow-GmbH/Jobs
Looking for hosting? http://hostingezpublish.com
-----------------------------------------------------------------------------
GMT +01:00 Hannover, Germany
Web: http://www.xrow.com/

Esben Maaløe

Tuesday 24 June 2003 5:55:09 am

Thanks Björn - that is certainly more elegant.