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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |