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.


In reply to Re: Template Toolkit Cache by ikegami
in thread Template Toolkit Cache by avo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.