Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.)

Steve
--

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-18 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found