<?xml version="1.0" encoding="windows-1252"?>
<node id="655845" title="Re^2: Abstracting away layout details when using large HTML::Template-based sites?" created="2007-12-08 12:31:46" updated="2007-12-08 07:31:46">
<type id="11">
note</type>
<author id="194370">
skx</author>
<data>
<field name="doctext">
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;For the simple case which I've presented it does work fine, but the general problem of recursively expanding templates doesn't.&lt;/p&gt;
&lt;p&gt;One thing that I notice is if you have a &lt;tt&gt;layout.tmpl&lt;/tt&gt; like this:&lt;/p&gt;
&lt;code&gt;
&lt;html&gt;
 &lt;head&gt;
  &lt;title&gt;&lt;!-- tmpl_var name='title' --&gt;&lt;/title&gt; 
 &lt;/head&gt;
 &lt;body&gt;
  &lt;!-- tmpl_include name='page.inc' --&gt;
 &lt;/body&gt;
&lt;/html&gt;
&lt;/code&gt;
&lt;p&gt;Things work!  The page.inc containing:&lt;/p&gt;
&lt;code&gt;
&lt;h2&gt;&lt;!-- tmpl_var name='title' --&gt;&lt;/h2&gt;
&lt;/code&gt;
&lt;p&gt;Is correctly processed via this code:&lt;/p&gt;
&lt;code&gt;
my $template = HTML::Template-&gt;new( filename =&gt; 'layout.tmpl' );
$template-&gt;param( title =&gt; "Steve" );
print $template-&gt;output();
&lt;/code&gt;
&lt;p&gt;So suddenly my problem is reduced to including variable files!  Unfortunately a similar lack of recusive support means this doesn't work:&lt;/p&gt;
&lt;code&gt;
 &lt;!-- tmpl_include name='&lt;!-- tmpl_var name='filename' --&gt;'&gt;
&lt;/code&gt;
&lt;p&gt;But via a filter I can get this same aim:&lt;/p&gt;
&lt;code&gt;

#
#  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!
#
#  &lt;!-- tmpl_var name='title' --&gt; 
#
#
$ENV{'page'} = 'page.inc';
my $template = HTML::Template-&gt;new(filename =&gt; 'layout.tmpl',
                                   filter =&gt; \&amp;filter );
$template-&gt;param( title =&gt; "Something here" );

print $template-&gt;output();
&lt;/code&gt;
&lt;p&gt;I don't know whether to feel pleased or dirty ..&lt;/p&gt;&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-194370"&gt;
&lt;a href="http://www.steve.org.uk/"&gt;Steve&lt;/a&gt;&lt;br/&gt;
-- &lt;br/&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
655823</field>
<field name="parent_node">
655840</field>
</data>
</node>
