[Templates] TT Speed Question

Mark Fowler mark@twoshortplanks.com
Wed, 8 Jan 2003 15:15:14 +0000 (GMT)


"Hann, Brian" <Brian.Hann@umb.com>

> What do you think, does this have any validity?

The most important thing with TT performance, the thing that most people
get wrong, is that you need to have one Template object that persists
between requests.

This means doing something like:

  our $template ||= Template->new();
   ... or ...
  $My::Class::Name::template ||= Template->new();

Not

  my $template = Template->new();
    ... or ...
  our $template = Template->new();
    ... or ...
  $My::Class::Name::template = Template->new();

If you do the former then the Template Toolkit will cache the template
generation code it creates for each template for next time the template
runs.  I.e. The first time TT parses a template it creates perl code that
will create the template output which it runs to get the output.  The
second time it needs to 'run' a template if (and only if) you're using the
same Template object) it simply executes the code again - it doesn't have
to do all that slow processing stuff.

Are you sure the test is doing this?

-- 
#!/usr/bin/perl -T
use strict;
use warnings;
print q{Mark Fowler, mark@twoshortplanks.com, http://twoshortplanks.com/};