Re: [htmltmpl] Select/option How to set "selected"?
Brought to you by:
samtregar
From: Mathew R. <mat...@re...> - 2004-05-06 06:25:20
|
> Matthew, I don't understand what's wrong with Jason's example. If DBI = yields > a result in the format HTML::Template understands, what's wrong with = taking > advantage of that? How would you change his example to make it more = indirect > and abstract? I would do something like: { my $sql_result =3D $db->fetch... my @output; foreach (@{$sql_result}) { my $tmp; $tmp->{name} =3D $_->[0]; if ($_->[0] =3D=3D 'fred') { $tmp->{selected} =3D 1; } push @output, $tmp; } template->param( 'some_loop' =3D> \@output); } this makes the 'some_loop' TMPL_LOOP become reasonably abstract, as used = below... > I love HTML::Template. Especially in combination with = CGI::Application. But, > sometimes a database structure naturally lends itself to a form = structure. > Doesn't it? Why is it wrong to pump the SQL output directly to > HTML::Template? nothing is wrong with it per se... its all a matter of opinion... I personally like the fact that I can do something like list with the = TMPL_LOOP: <TMPL_LOOP some_loop> <TMPL_IF EXPR=3D"(__counter__ % 3) =3D=3D 1"> <tr valign=3Dtop> </TMPL_IF> <td width=3D33% align=3Dcenter valign=3Dtop class=3Dtext> ... some info... <TMPL_IF selected> .... entry selected </TMPL_IF> </td> <TMPL_IF EXPR=3D"(__counter__ % 3) =3D=3D 0"> </tr> <tr> <td colspan=3D3> <spacer> </td> </tr> </TMPL_IF> </TMPL_LOOP> Basically this stuff takes the loop, then formats the output into three = columns and multiple rows. You can see here that the loop is no longer a combobox, but anthing that = can be drawn using a table. Thus, not tie'ing the output of the SQL to a specific format, allows me = to use the same loop definition within: 1) as above 2) combobox 3) list of checkboxes / radiobuttons 4) arbitrary <UL> styled list where I color the selected item Note, I also use a variation of the above code to display a picture = gallery -> the Perl code doesn't need to care how I format the output = (which would normally be done with nested TMPL_LOOP's). This explain? Mathew >=20 > Mark >=20 > ----- Original Message -----=20 > From: "Mathew Robertson" <mat...@re...> > To: <htm...@li...> > Sent: Monday, May 03, 2004 8:26 PM > Subject: Re: [htmltmpl] Select/option How to set "selected"? >=20 >=20 > > What's really neat if you do it like that is you can use DBI's > > fetchall_arrayref method and be really snazzy: > > > > my $template =3D HTML::Template->new( 'filename' =3D> 'file.TMPL' ); > > my $sth =3D $dbh->prepare( <<_SQL_ ); > > SELECT val, > > text, > > (if then else to return 'selected' or '') AS selected > > FROM table > > WHERE whatever... > > _SQL_ > > $sth->execute(); > > $template->param( 'OCCUPATION_LOOP' =3D> $sth->fetchall_arrayref( {} = ) ); > > ## Note that the fetchall_arrayref doesn't save you computing time = -- > > ## it's only a shortcut and does the same thing as your loop to go > > ## through the results > > --- > > > > What's neat about this, too is that it simplifies the template code, = as > > Puneet laid out. >=20 > 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. >=20 > The purpose of H::T is to decopule the data from the presentation. > Mathew >=20 >=20 > ------------------------------------------------------- > This SF.Net email is sponsored by: Oracle 10g > Get certified on the hottest thing ever to hit the market... Oracle = 10g. > Take an Oracle 10g class now, and we'll give you the exam FREE. > http://ads.osdn.com/?ad_id149&alloc_id=8166&op=3Dick > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users >=20 > |