in reply to Best practices: Generating HTML content

See your thread from earlier this month where several Monks recommended that you use templates to separate your HTML from your Perl code.

If I really really need sinppets of HTML in my program for some reason (e.g. because I am avoiding using a JavaSCript framework on the front end), I still use Template to generate them, so the raw HTML is kept apart. In such a case you would have the code you showed in its own something.tt template file, that would be processed with output stored in a reference to a scalar variable. The technique is somewhat convoluted as you wind up with a number of template files, so naming them sensibly and storing them in a logical file structure is important, but for me it's worth it since I don't like to feel dirty.


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Best practices: Generating HTML content
by Dallaylaen (Chaplain) on Dec 24, 2017 at 15:31 UTC
    Template can also render in-memory templates via
    my $var; my $template = Template->new; $template->process( \'In-place [% foo %] query', { foo => 42 }, \$var +);

    It comes with a performance penalty though as TT does not cache compiled in-memory templates by default.

    UPD As 1nickt mentions, the penalty can be overcome by compiling the template explicitly:

    my $tt = Template->new; my $compiled = $tt->template( \'In-place [% foo %] query' ); $template->process( $compiled, { foo => 42 }, \$var );

      Hi Dallaylaen,

      TT does not cache compiled templates. This can be overcome but not easily.

      Just to clarify, I assume you mean that the Template Toolkit does not cache compiled templates *persistently* if they originated as a reference to a string, as you showed.

      See the documentation on "Caching and Compiling Options".

      You can cache compiled templates in memory explicitly without even processing them using template():

      $ perl -MTemplate -wE ' my $tt = Template->new; my $compiled = $tt->template(\"In-place [% foo %] query\n"); my $answer = 40; $tt->process( $compiled, {foo => $answer++} ) for 0..2; ' In-place 40 query In-place 41 query In-place 42 query
      And of course you can cache persistently between program runs if you use the right configuration options (and use a template file or filehandle):
      my $tt = Template->new( INCLUDE_PATH => '/some/path', COMPILE_DIR => '/other/path', COMPILE_EXT => '.compiled', ); my $file = 'foo.tt'; my $data = { foo => 42 }; $tt->process( $file, $data ) or die $tt->error; # Template will be compiled and cached at '/other/path/foo.tt.compiled +' # and that file will be used until '/some/path/foo.tt' changes
      I'm sure you knew this, just clarifying for future readers...


      The way forward always starts with a minimal test.

        Thanks 1nickt,

        I only meant im-memory caching during the application lifetime. Not between runs (that's for sure not what one wants to do in a module).

        Upon reading your reply, I rushed to revisit my code that dealt with caching scalar reference templates. What I found there was exactly my $compiled = $tt->template(\$in_memory); sequence - at which I arrived after trying to utilize Template::Provider.

        So caching compiled templates is indeed simple. It's just not quite obvious from documentation. And the possible speed penalty is a pitfall that's worth mentioning.