[Templates] TT and mod_perl tips
Chris Winters
chris@cwinters.com
Fri, 17 Oct 2003 08:44:25 -0400
Jose Luis Mart=EDnez wrote:
> I'm going to migrate all my scripts from mod_cgi to mod_perl (for apach=
e
> 1.3). Can you give me some tips on what are the best practices for
> having TT on a mod_perl server?
>=20
> I was thinking of loading TT when apache starts (our applications
> allways use TT). Is this safe? Should I consider something special?
>=20
> How should I use the template object? I've seen in the list that someon=
e
> recomended creating only one instance of the template object, and have
> it global with 'our'. Can this cause problems?
The idea is that you want to keep using the same processor object=20
so you can take advantage of caching and perform any object=20
initialization only once.
Instead of using 'our' you can make it a package lexical with=20
'my' and create a method for controlled access (all code off the=20
top of my head, untested, etc.):
package My::Template;
use strict;
use Template;
my ( $TEMPLATE );
sub init {
my ( $class ) =3D @_;
$TEMPLATE =3D Template->new( ... );
}
sub get_processor {
return $TEMPLATE;
}
And then initialize it in a 'startup.pl':
#!/usr/bin/perl
use strict;
use My::Template;
My::Template->init();
And then use it in one of your handlers:
package My::Handler;
use strict;
use My::Template;
sub handler($$) {
my ( $class, $r ) =3D @_;
my ( $html );
my $text =3D $class->get_template_text( ... );
my $vars =3D $class->get_template_vars( ... );
My::Template->get_processor()->process(
$text, $vars, \$html );
$r->print( $html );
...
}
HTH
Chris
--=20
Chris Winters (chris@cwinters.com)
Building enterprise-capable snack solutions since 1988.