Nested Templates
A neat feature of the
is actually a template variable, just as some_color is a template variable. So if
you have another template with a
all of the
lets you embed the text of one template file into another template. So, we could
have another template called wholePage.ihtml that contains the text:
parse()
function is that the MyOutput handle that it createdis actually a template variable, just as some_color is a template variable. So if
you have another template with a
{MyOutput}
tag, when you parse that second template,all of the
{MyOutput}
tags will be replaced with the parsed text from MyOutput. Thislets you embed the text of one template file into another template. So, we could
have another template called wholePage.ihtml that contains the text:
Sorry you didn’t win. But if you had won, we would have told you:
{MyOutput}
{MyOutput}
And after wholePage.ihtml is parsed, the final output would be:
Sorry you didn’t win. But if you had won, we would have told you:
Congratulations! You won a new blue Honda Prelude!
Congratulations! You won a new blue Honda Prelude!
Here is the PHP code to parse both templates:
<?php
$t
= new Template("/home/mydir/mytemplates/");
// These three lines are the same as the first example:
$t->set_file("MyFileHandle","MyTemplate.ihtml");
$t->set_var("some_color",$my_color);
$t->parse("MyOutput","MyFileHandle");
// (Note that we don't call p()
//here, so nothing gets output yet.)
// Now parse a second template:
$t->set_file("WholeHandle","wholePage.ihtml");
// wholePage.ihtml has "{MyOutput}" in it
$t->parse("MyFinalOutput","WholeHandle");
// All {MyOutput}'s get replaced
$t->p("MyFinalOutput");
// output the value of MyFinalOutput
?>
The last two lines with calls to
shorthand function
parse()
and p()
can actually be combined using theshorthand function
pparse()
by replacing the last two lines with :
<?php
pparse("MyFinalOutput","SecondHandle");
?>
Another feature of PHPLIB Template is that the functions
can also accept multiple sets of values at a time by passing an array of
handle/value pairs. Here are examples:
set_file()
and set_var()
can also accept multiple sets of values at a time by passing an array of
handle/value pairs. Here are examples:
<?php
$t
->set_file(array(
"pageOneHandle" => "pageone.ihtml",
"pageTwoHandle" => "pagetwo.ihtml"));
$t->set_var(array(
"last_name" => "Gates",
"first_name" => "Bill",
"net_worth" => $reallybignumber));
?>