Thanks for the reply, as you suggest my content will be coming from an external source, along with all other page data. In this case it will be coming from a database.

Your solution of moving things around doesn't really apply terribly well when I consider how I would be using this in practise I'm afraid.

For the simple case which I've presented it does work fine, but the general problem of recursively expanding templates doesn't.

One thing that I notice is if you have a layout.tmpl like this:

<html> <head> <title><!-- tmpl_var name='title' --></title> </head> <body> <!-- tmpl_include name='page.inc' --> </body> </html>

Things work! The page.inc containing:

<h2><!-- tmpl_var name='title' --></h2>

Is correctly processed via this code:

my $template = HTML::Template->new( filename => 'layout.tmpl' ); $template->param( title => "Steve" ); print $template->output();

So suddenly my problem is reduced to including variable files! Unfortunately a similar lack of recusive support means this doesn't work:

<!-- tmpl_include name='<!-- tmpl_var name='filename' -->'>

But via a filter I can get this same aim:

# # Replace ### with environmental variable variable 'page' # sub filter { my ($text_ref ) = shift; my $val = $ENV{'page'}; $$text_ref =~ s/###/$val/g; }; # # Load 'layout.tmpl' - and have that include 'page.inc'. # # THis will expand the following in *both* files! # # <!-- tmpl_var name='title' --> # # $ENV{'page'} = 'page.inc'; my $template = HTML::Template->new(filename => 'layout.tmpl', filter => \&filter ); $template->param( title => "Something here" ); print $template->output();

I don't know whether to feel pleased or dirty ..

Steve
--

In reply to Re^2: Abstracting away layout details when using large HTML::Template-based sites? by skx
in thread Abstracting away layout details when using large HTML::Template-based sites? 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.