It's my understanding that caching compiled templates will speed up
performance, as will using a global Template object instead of a local
one. Please take a look at the sample code below and tell me if I'm on
the right track here. This seems to work fine, but I just wanted to
confirm that this is the correct way of doing this.

The main thing I'm unsure of is (and this is more a mod_perl question
than a TT one): I know that mod_perl keeps globals alive, but will it
call the Template->new() method on every request, thus necessitating
something like the "||=" operator, or can I simply use "=" and mod_perl
will take care of the rest?

Thanks for your help.

-Tim


package Apache::Test::Cache;

use strict;
use Apache::Constants qw( :common );
use Template;

our $VERSION = '0.01';

our $template = Template->new( {
    INCLUDE_PATH    =>
'/usr/local/apache/perl/tt/html:/usr/local/apache/perl/tt/html/include',
    CACHE_SIZE      =>  64,
    COMPILE_DIR     =>  '/usr/local/apache/perl/tt/cache',
    COMPILE_EXT     =>  '.ttc',
} );

sub handler {
    my $r = shift;

    my %data = (
        foo =>  'bar',
        uri =>  $r->uri,
    );

    my $file = $r->path_info;
    $file =~ s{^/}{};

    $r->content_type('text/html');
    $r->send_http_header;

    $template->process( $file, \%data, $r )
        or return fail( $r, SERVER_ERROR, $template->error );

    return OK;
}

sub fail {
    my ( $r, $status, $message ) = @_;
    $r->log_reason( $message, $r->filename );
    return $status;
}





Reply via email to