http://qs1969.pair.com?node_id=72573


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 } );

user_form.tmpl

<p>Welcome [% user.name %]. 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.

Chris
M-x auto-bs-mode