On Mon, 15 Nov 1999, Leslie Mikesell wrote:
> What I'm looking for is a 'nestable' way of handling the logic
> flow and HTML construction that will allow a page to be used
> as a stand-alone item (perhaps displayed in a frameset) or
> included in another page, but when it is included I'd like to
> have the option of letting its execution return a status
> that the including page could see before it sends out any
> HTML.
Ok, here's how you can do this with HTML::Template. I can't really
address the solution as "pages that execute" since with HTML::Template,
only scripts execute. But anyway:
First, define your component that you want to use both stand-alone and as
an included component:
component.tmpl:
<TMPL_IF NAME="STAND_ALONE_MODE">
<HTML>
<HEAD>
<TITLE>I'm all alone...</TITLE>
</HEAD>
<BODY>
</TMPL_IF>
Some stuff that users will love to pay for : <BR>
by the way, my name is <TMPL_VAR NAME="NAME"><P>
<TMPL_IF NAME="STAND_ALONE_MODE">
</BODY>
</HTML>
</TMPL_IF>
Now, when you want to use it stand-alone:
stand_along.cgi:
my $template = HTML::Template->new(filename => 'component.tmpl');
$template->param(STAND_ALONE_MODE => 1);
$template->param(NAME => 'Sam');
print "Content-Type: text/html\n\n";
print $template->output;
Or within a larger entity:
larger.tmpl:
<HTML>
<HEAD>
<TITLE>This is a big ol' page</TITLE>
</HEAD>
<BODY>
Some stuff...<P><P>
The component: <TMPL_VAR NAME="COMPONENT"><P>
</BODY>
</HTML>
together.cgi:
my $component = HTML::Template->new(filename => 'larger.tmpl');
$template->param(STAND_ALONE_MODE => 0);
$template->param(NAME => 'Sam');
my $template = HTML::Template->new(filename => 'larger.tmpl');
$template->param(COMPONENT => $component_template->output());
print "Content-Type: text/html\n\n";
print $template->output;
Now to support the whole "returning an error code" idea you'd need to
wrap component.tmpl in a module that knows when to return an error.
For something that approaches this level of complexity with
HTML::Template, take a look at HTML::Pager.
As you can see, HTML::Template doesn't send any output prematurely to
the user - the enclosing CGI or module can do whatever it wants with
the output before printing it.
So, did I totally misunderstand what you're looking for, or have I
answered your prayers?
-sam