On Mon, Jan 10, 2005 at 06:33:54PM +1100, Michael Kraus wrote: > I'm curious about using HTML::Template to pre-fill in form field values. ... > > What I'm currently using is this method > > <input name="my_input_box"<TMPL_IF NAME="my_input_box_value"> > value="<TMPL_VAR NAME="my_input_box_value">"</TMPL_IF>> > > This works great for filling in text boxes. However, how can this be > done for select boxes (drop-down lists)? > > Currently I've got a select box that looks like this: > ---CUT--- > <select name="my_select"> > <TMPL_LOOP NAME="my_select_loop"> > <option value="<TMPL_VAR NAME="id">"><TMPL_VAR > NAME="description"></option> > </TMPL_LOOP> > </select> > ---CUT--- > > I can't see how HTML::Template would cope with this in an automagic way. > (???) > > I'm guessing, I could use ecma-script/j-script/javascript in the HTML > with variables set to HTML::Template variables. > ... > Or something like the following: > > In Template: > ---CUT--- > <select name="my_select"> > <TMPL_LOOP NAME="my_select_loop"> > <option value="<TMPL_VAR NAME="id">"<TMPL_IF NAME="selected"> > selected</TMPL_IF>><TMPL_VAR NAME="description"></option> > </TMPL_LOOP> > </select> > ---CUT--- > > In code: > ---CUT--- > for $item (@$my_select_loop) { > $item->{selected} = 1 if $item->{id} eq $cgi->param("id"); > } > [...] > $template->param("my_select_loop" => $my_select_loop); > print $cgi->header, $template->output; > ---CUT--- > > Is there a simpler way, or am I just asking too much and should stop > whinging and continue coding? :)
That would be the proper way. However, I've been known to cheat. It turns out you can have duplicate values in pull-down lists. Therefore, in most cases, if you find it aesthetically acceptable, you can simply do this: <select name="my_select"> <TMPL_IF NAME="my_select_value"> <option selected value="<TMPL_VAR NAME="my_select_value">"><TMPL_VAR NAME="my_select_description"></option></TMPL_IF> <TMPL_LOOP NAME="my_select_loop"> <option value="<TMPL_VAR NAME="id">"><TMPL_VAR NAME="description"></option> </TMPL_LOOP> </select> The effect is that your selected value appears twice in the list - once at the top, selected, and once down in the body. I have not seen either a browser or a user balk at this yet, though I personally find it a bit less aesthetically pleasing. -- Clifton -- Clifton Royston -- [EMAIL PROTECTED] Tiki Technologies Lead Programmer/Software Architect Did you ever fly a kite in bed? Did you ever walk with ten cats on your head? Did you ever milk this kind of cow? Well we can do it. We know how. If you never did, you should. These things are fun, and fun is good. -- Dr. Seuss ------------------------------------------------------- The SF.Net email is sponsored by: Beat the post-holiday blues Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek. It's fun and FREE -- well, almost....http://www.thinkgeek.com/sfshirt _______________________________________________ Html-template-users mailing list Html-template-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/html-template-users