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

Re: Template Toolkit 2

by lachoy (Parson)
on Apr 14, 2001 at 17:19 UTC ( #72573=note: print w/replies, xml ) Need Help??

in reply to Template Toolkit 2

Just a couple little things. About caching: TT2 compiles a template to a Perl subroutine and caches it, which is very fast indeed! It also allows you to save this Perl subroutine to disk so the next time a system like mod_perl starts up you don't even have to parse the template. Sweet! (Yes, I'm a big fan of TT.)

Another nifty benefit that you touched on briefly is the consistent notation for variable access. For instance you can start out a template using a hash user:

my $tmpl = Template->new(); $tmpl->process( 'user_form.tmpl', { user => { name => 'Bruce Springsteen', last_login => '2000-01-01 05:45:00' }, common_date_format => \&my_date_format } );


<p>Welcome [% %]. Your last login was [% common_date_format( user.last_login ) %].</p>

Later, you might want to make user an object which has the methods name() and last_login():

my $user = User->fetch( 'theboss' ); my $tmpl = Template->new(); $tmpl->process( 'user_form.tmpl', { user => $user, common_date_format => \&my_date_format } );

This requires no modification to your template, which makes refactoring a larger system a more palatable task.

M-x auto-bs-mode

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2023-12-06 04:54 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (29 votes). Check out past polls.