Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Tag libs for Template Toolkit

by johnnywang (Priest)
on Sep 14, 2004 at 22:32 UTC ( #390998=perlquestion: print w/ replies, xml ) Need Help??
johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Hi, is there something for Template Toolkit that is similar to JSP customer tag libs? Java's Struts provides many taglibs that make it very easy to produce html widgets such as options/selects/radio/checkbox, etc. in the form:
<html:text property="user"/>
which will generate and fill in <input type="text" name="user" value="..."/>. I'm wondering whether TT has something similar. Right now I'm doing everything by hand each time, using something like %FOREACH %. Will the CGI plugin for TT help? since CGI.pm has some of those html widget functions? Thanks.

Comment on Tag libs for Template Toolkit
Download Code
Re: Tag libs for Template Toolkit
by borisz (Canon) on Sep 14, 2004 at 22:47 UTC
    You are asking for Splash: Splash!.
    Boris
Re: Tag libs for Template Toolkit
by perrin (Chancellor) on Sep 15, 2004 at 00:37 UTC
    JSP taglibs are similar to TT plugins. You can do pretty much anything with a plugin. The CGI plugin does handle HTML form inputs, but you can also write your own to do something specific to your application.
Re: Tag libs for Template Toolkit
by Your Mother (Canon) on Sep 15, 2004 at 05:22 UTC

    I'd say the CGI plugin is better than something like this too: <html:text property="user"/> b/c the attributes match up better; property? "What HTML attribute is that?" one might ask. Here's the CGI TT2 for it.

    [% USE CGI # goes anywhere %] [% CGI.textfield( name => 'user' ) %]
    Another advantage being you can embed all the attr you want.
    [% CGI.textfield( name => 'user', onmouseover => 'this.focus()', style => 'width:12em;', default => user_obj.user || CGI.param('user') ) %]

    Also, check out how to do WRAPPERs, INCLUDEs, and define your own FILTERs. TT2 is *extremely* powerful for what it will let you do in those regards. In fact this got me a bit excited to spark up more sharing of this stuff. I'll try to post a handful of the TT2 snippets/wrappers I use in a day or two.

Re: Tag libs for Template Toolkit
by jeffa (Chancellor) on Sep 15, 2004 at 16:31 UTC

    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)
    

      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)
        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://390998]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-12-27 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls