Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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 has some of those html widget functions? Thanks.

Replies are listed 'Best First'.
Re: Tag libs for Template Toolkit
by borisz (Canon) on Sep 14, 2004 at 22:47 UTC
    You are asking for Splash: Splash!.
Re: Tag libs for Template Toolkit
by Your Mother (Bishop) 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 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 jeffa (Bishop) 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="[% %]" value="[% text.value %]" /> [% END %]


    (the triplet paradiddle with high-hat)

      Using the 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: ./ 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="[% %]" name="[% %]" selected="[% o.selected %]" /> [% END %]
        But ... you are right. is very nice. By the way ... my point was TIMTOWTDI. That's all.


        (the triplet paradiddle with high-hat)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2020-10-20 06:25 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (209 votes). Check out past polls.