Re: [htmltmpl] Select/option How to set "selected"?
Brought to you by:
samtregar
From: petersm <pe...@ve...> - 2004-05-04 14:31:33
|
Puneet Kishor wrote > > since there are more than three ways to do it, I am actually going to > dissent here. Personally, I don't like CGI.pm. I think it weighs a lot, > and most of the time I use it (and I do), it mainly to extract the > params. I am stuck with it in a recent project because I am using > CGI::Session, and I am not sure if that will work with CGI::Simple (can > anyone tell me if it will and how?). There has been some recent discussion on the CGI::Application list of just this issue. It seems that CGI::Session does a pretty strict test to make sure that it has a CGI.pm object and not just a compatible object. I think some work is underway to change this so that it does some tests for the param() and cookie() methods. So, not right now, but maybe not too much later. > > Except that you have now tied your SQL statement directly to HTML > > syntax; in which case... what is the point of using H::T -> you could > > generate HTML output faster if you created an SQL statement which > > generated HTML directly. > > > > The purpose of H::T is to decopule the data from the presentation. how about something like this that decouples the SQL from the HTML but is still relatively simple (and uses XHTML instead of HTML ) in perl . . my $sth = $dbi->prepare(qq(SELECT val, text, (val = "$selected") AS selected FROM whatever WHERE whatever)); my $tmpl = HTML::Template->new(); $tmpl->param(occupations => $sth->fetchall_arrayref( {} ); in the tmpl (for a dropdown select box) . . <select name='whatever'> <TMPL_LOOP NAME=OCCUPATION_LOOP> <option value="<TMPL_VAR NAME=VAL>" <TMPL_IF NAME="SELECTED">selected='selected'</TMPL_IF>><TMPL_VAR NAME=TEXT></option> </TMPL_LOOP> </select> or in the tmpl for a group of checkboxes, (or radio buttons) . . <TMPL_LOOP NAME=OCCUPATION_LOOP> <input type='checkbox' value="<TMPL_VAR NAME=VAL>" name='whatever' <TMPL_IF NAME="SELECTED">checked='checked'</TMPL_IF> /><TMPL_VAR NAME=TEXT><br /> </TMPL_LOOP> this way the same SQL creates a structure that is independent of the HTML but still not so much so that it's hard to work with. Michael Peters Venzia |