I just had a peek at the source. The relevant code is in Template::Provider. The compiled template cache code is highly dependent on the presence of a file name.
Your best bet might be to subclass Template::Provider, overriding _template_modified($path) and _template_content($path) to fetch the template from memory instead of fetching the template from disk. That will allow you to use a file name to fetch the template (albeit to a virtual file), which will allow TT2 to cache the compiled template.
Update: Subclassing would look something like:
use strict;
use warnings;
BEGIN {
package Template::Provider::Memory;
use Template::Provider qw( );
BEGIN { our @ISA = 'Template::Provider'; }
sub _init {
my ($self, $params) = @_;
$self->{ MEM_DRIVE } = $params->{ MEM_DRIVE };
$self->SUPER::_init($params);
}
sub _template_modified {
my ($self, $path) = @_;
if (!exists( $self->{ MEM_DRIVE }{ $path } )) {
return undef;
}
return $self->{ MEM_DRIVE }{ $path }[0];
}
sub _template_content {
my ($self, $path) = @_;
my $data;
my $mod_date;
my $error;
if (exists( $self->{ MEM_DRIVE }{ $path } )) {
$mod_date = $self->{ MEM_DRIVE }{ $path }[0];
$data = $self->{ MEM_DRIVE }{ $path }[1];
} else {
$error = "$path: Not found";
}
return (wantarray
? ( $data, $error, $mod_date )
: $data
);
}
$INC{'Template/Provider/Memory.pm'} = 1;
}
use Template qw( );
use Template::Provider::Memory qw( );
my %mem_drive = (
'index.tt2' => [ time(), 'Hello World!' ],
);
my $tt = Template->new({
LOAD_TEMPLATES => [
Template::Provider::Memory->new({ MEM_DRIVE => \%mem_drive }),
Template::Provider->new(),
],
COMPILE_DIR => '/tmp/ttc',
COMPILE_EXT => '.ttc',
});
$tt->process('index.tt2')
or die($tt->error(), "\n");
That was tough to decipher! Runs, but it doesn't create the compiled file.
|