I've got a fairly complex web application which I've almost completely ported from a series of static cgi scripts to using CGI::Application.

The one outstanding task I've yet to cover is how to handle nested HTML::Template files.

In my application I have a global "layout" template, as well as run-mode-specific templates. We could pretend that the layout would look like this, although in truth there is more divitus:

<html>
  <head> <title> <!-- tmpl_var name='title' --></title>
  <body>
    ..
     <div id="content">
       <!-- tmpl_include name='###' -->
     </div>
  </body>
</html>

The magic here is obviously the insertion of a second template, the run-mode-specific page template, which is carried out dynamically.

HTML::Template doesn't allow loading dynamic include-files so I've had to be sneaky - which is often a sign I'm going down a bad route:


sub mk_include_filter
{
    my $page = shift;
    return sub {
        my $text_ref = shift;
        $$text_ref =~ s/###/$page/g;
    };
}

sub load_page_layout
{
    my( $self, $page, %options ) = ( @_ );

    my $layout = "../templates/layouts/default.template";
    my $l = HTML::Template->new( filename => $layout,
                                 %options,
                                 filter => mk_include_filter($page) );


   # ...
   return ($l);
}

This allows me to run code such as:

my $template = $self->load_page_layout( "about.tmpl" );

The return value is essentially the union of the layout-template and the page-template, and allows values to be set which are defined in either:

$template->param( title => "The title, as defined in the layout",
                  page_specific => "A page-specific value" );

All this works, and works well, but I wonder if there is a better approach to handling dynamic sub-templates, or similar?

Specifically my problem is that I cannot use this approach and migrate to HTML::Template::Compiled because the filter doesn't work as expected. Pages return completely wrong values..

(Edit: The obvious solution is to duplicate the global-layout into each page, if possible I'd like to avoid that as it makes updating the layout a challenge. Although perhaps I could use the C-preprocessor to build complete pages from the layout + sub-pages. I might explore this if there is no "obvious" answer to my query.)

--

In reply to Nested HTML::Template usage by skx

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.