[Templates] Hooking into template file accesses
Garrett, Philip (MAN-Corporate)
Philip.Garrett@manheim.com
Tue, 28 Feb 2006 12:04:34 -0500
> -----Original Message-----
> From: templates-admin@template-toolkit.org
[mailto:templates-admin@template-toolkit.org] On Behalf Of Paul LeoNerd
Evans
> Sent: Monday, February 27, 2006 8:20 PM
> To: templates@template-toolkit.org
> Subject: [Templates] Hooking into template file accesses
>=20
[snip]
>
> 2. It tracks the latest mtime of any file (or other data) used
during
> the building of the page, in order to set the "Last_Modified:"
> header on reply and to implement the "If_Modified_Since:" logic.
You could subclass Template::Provider::fetch and add your mtime checking
code to it. (*untested code*) e.g.
package My::Mtime::Provider;
use base qw(Template::Provider);
sub init_mtime {
my ($self) =3D @_;
$self->{_my_mtime} =3D 0;
}
sub check_mtime {
my ($self,$doc) =3D @_;
# remember this mtime if it's newer than
# anything else we've seen
if ($doc && $doc->modtime > $self->{_my_mtime}) {
$self->{_my_mtime} =3D $doc->modtime;
}
}
=20
sub get_mtime {
my ($self) =3D @_;
return $self->{_my_mtime};
}
sub fetch {
my $self =3D shift;
my ($doc,$reason) =3D $self->SUPER::fetch(@_);
$self->check_mtime($doc);
return ($doc,$reason);
}
1;
Then you install the provider like this:
my %config =3D ( COMPILE_DIR =3D> '/wherever', @other_opts );
my $provider =3D My::Mtime::Provider->new(\%config);
my $template =3D Template->new(%config,
LOAD_TEMPLATES =3D> [$provider]);
Then, every time you process a template, do this:
$provider->init_mtime();
$template->process($template_name) || die $template->error;
my $mtime =3D $provider->get_mtime();
I hope the code doesn't have too many bugs in it.
HTH
Philip