srins has asked for the wisdom of the Perl Monks concerning the following question:

hi,
I need to define selected option in select option menu from hash in template toolkit. I have an hash in format
my @add=( { gui=>'E', value=>'Default ATS Location', name =>'res_op_file_loc', label=>'Result Output File ', default =>'Default ATS Location' }, { 'gui' => 'R', 'value' => 'ON', 'enum' => [ 'ON', ' OFF' ], 'name' => 'email_notif', 'label' => 'E-mail notification', 'default' => 'ON' } );
I have checked if gui=>E,it is text box and if gui=>"R",it is drop down ie option menu.
How to define default selected option for select option menu by using default variable from hash ie default=>'ON' in option menu as selected.How to do it in template toolkit. I have sample template for text box and select option menu.
<table> [% FOREACH element = add %] [% IF element.gui == "E" %] <tr> <td width="218" height="16"><font face="Times New Roman">[% elemen +t.label %] </font></td> <td width="713" height="16"> <font face="Times New Roman"> <input type="text" name="[%element.name %]" value="[%element.default +%]" size=" 58"> </td> </tr> [% ELSIF element.gui == "R" %] <tr> <td width="218" height="22"> <font color="#FF0000" face="Times New Roman">[% element.label %]</ +font></td> <td width="713" height="22"><font face="Times New Roman"> <select name="[% element.name %]" style="color: #FF0000" clas +s="sideeff ectcolor" id="sideeffectcolor">"> [% FOREACH option = element.enum %] <option value="[% option %]"> [% option %] </option> [% END %] </select>* </font></td> </tr> [% END %] [% END %] </table>
how to modify my code using template toolkit, by setting default option for option menu from default variable from hash.Can any body help me in this regard. Thanks, srins.

Replies are listed 'Best First'.
Re: how to define selected option in select option menu in template toolkit from hash
by davidrw (Prior) on Nov 09, 2005 at 23:40 UTC
    just need to conditionally make one of them be like <option value="foo" SELECTED>foo</select> like this:
    [% FOREACH option = element.enum %] <option value="[% option %]" [% 'SELECTED' IF option == elemen +t.default %] > [% option %] </option> [% END %]