Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Tag libs for Template Toolkit

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


in reply to Tag libs for Template Toolkit

TIMTOWTDI! Run this through tpage:

[% INCLUDE input type => 'text' name => 'foo' value => 'Hello world' %] [% BLOCK input %] <input type="[% type %]" name="[% name %]" value="[% value %]" /> [% END %]
Although i usually just type the form elements out explicitly. There really is not a whole lot of gain in abstracting these thingies, unless you are generating them dynamically. Even then, it's just as easy to use something like:
[% items = [ { name => 'foo' value='baz'} { name => 'bar' value='qux'} ] %] [% FOREACH text = items %] <input type="text" name="[% text.name %]" value="[% text.value %]" /> [% END %]

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^2: Tag libs for Template Toolkit
by Aristotle (Chancellor) on Sep 16, 2004 at 03:39 UTC

    Using the CGI.pm plugin is nice though, because it lets you do stickiness without extra provisions or clutter in the template. And while abstraction doesn't buy you much with simple type="text" fields, it's quite nice for select boxes.

    (I wrote about this quite a while ago.)

    Makeshifts last the longest.

      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)
      

        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://391235]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found