Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Tag libs for Template Toolkit

by jeffa (Bishop)
on Sep 16, 2004 at 18:22 UTC ( [id://391529]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Tag libs for Template Toolkit
in thread Tag libs for Template Toolkit

There are ways to avoid that unwanted clutter in your templates. I've only tested the following with MySQL, but it's pretty slick. Try it out in a shell: ./foo.pl state=42

use DBI; use CGI qw(param); use Template; my $dbh = DBI->connect( ... ); my $state_id = param('state'); my $states = $dbh->selectall_arrayref(' SELECT id,name, IF (id = ?, 1, 0) AS selected FROM state ',{Slice => {}}, $state_id); my $tt = Template->new; $tt->process(\*DATA, {states => $states}) || die $tt->error(); __DATA__ <select name="state"> <option value="">Pick A State, Any State</option> [% FOREACH state = states %] [% PROCESS option o = state %] [% END %] </select> [% BLOCK option %] <option value="[% o.id %]" name="[% o.name %]" selected="[% o.selected %]" /> [% END %]
But ... you are right. CGI.pm is very nice. By the way ... my point was TIMTOWTDI. That's all.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^4: Tag libs for Template Toolkit
by Aristotle (Chancellor) on Sep 17, 2004 at 20:06 UTC

    Your "Pick A State" complicates things a little, but still, compare your template to

    [% options = [ '' ]; cgi.popup_menu( 'state', options.merge( states ), '', { '' => 'Pick A State, Any State' } ); %]

    Update: ah, I didn't even notice that you were handling stickiness via the SQL query. I think that just goes to prove my point though. It is a very neat hack, granted — I might use it in a short script that's about as complex as what you have. However, you are coupling the SQL, the query parameter names and the template very tightly that way. If you do that sort of thing in 30 places of a larger codebase, it'll become annoying to maintain very quickly.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://391529]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-28 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found