note
skx
<p>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.</p>
<p>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.</p>
<p>For the simple case which I've presented it does work fine, but the general problem of recursively expanding templates doesn't.</p>
<p>One thing that I notice is if you have a <tt>layout.tmpl</tt> like this:</p>
<code>
<html>
<head>
<title><!-- tmpl_var name='title' --></title>
</head>
<body>
<!-- tmpl_include name='page.inc' -->
</body>
</html>
</code>
<p>Things work! The page.inc containing:</p>
<code>
<h2><!-- tmpl_var name='title' --></h2>
</code>
<p>Is correctly processed via this code:</p>
<code>
my $template = HTML::Template->new( filename => 'layout.tmpl' );
$template->param( title => "Steve" );
print $template->output();
</code>
<p>So suddenly my problem is reduced to including variable files! Unfortunately a similar lack of recusive support means this doesn't work:</p>
<code>
<!-- tmpl_include name='<!-- tmpl_var name='filename' -->'>
</code>
<p>But via a filter I can get this same aim:</p>
<code>
#
# 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();
</code>
<p>I don't know whether to feel pleased or dirty ..</p><!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-194370">
<a href="http://www.steve.org.uk/">Steve</a><br/>
-- <br/>
</div></div>
655823
655840