The class manages a set of variables which are text strings. These strings may contain references to other variables in the form of "{variable}". When parsed or substituted, a variable reference is being replaced by the value of that variable. For example, if you
<?php
$t = new Template;
$t->set_var("a", "defined as hugo");
$t->set_var("b", "the value of a is {a}");
print $t->subst("b")
?>
will print the value of a is defined as hugo.
A variable value may be defined manually by calling set_var("name", "value"); or it may be defined from a file by calling set_file("name","filename.ihtml");. In the latter case, the contents of the file are being loaded when needed (as late as possible) and set as the value of that variable.
A third way to define a variable value is to call set_block("parent", "block", "name");. In this case, the variable named parent is being searched for a block that starts with <!-- BEGIN block --> and ends with <!-- END block -->. This string is removed from the variable parent and assigned to the variable named block. In parent, a variable reference to name is placed instead. If the optional parameter "name" is left out, "block" is being used instead.
For example, if you
<?php
$t = new Template;
$t->set_var("a", "front matter
<!-- BEGIN b -->
this is block b
<!-- END b -->
end matter");
$t->set_block("a", "b", "bb");
?>
this will define the variable a as front matter {bb} end matter and the variable b as this is block b. All of this will become more clear when you set the instance variable debug to 7 in the following example and trace the variable accesses.
Use Template direcly or define a subclass of Template as needed.
Define a template file named page.ihtml as follows:
<html>
<head><title>{PAGETITLE}</title></head>
<body bgcolor="#ffffff">
<table border=1 cellpadding=4 cellspacing=0 bgcolor="#eeeeee">
<tr>
<td colspan=2><h1>{PAGETITLE}</h1></td>
</tr>
<tr>
<td>{OUT}</td>
<td>Content<br>{UNDEFINED_VARIABLE}</td>
</tr>
</table>
</body>
</html>
This file contains a reference to the variable PAGETITLE and a reference to the variable named OUT. Another reference to the variable named UNDEFINED_VARIABLE is never resolved. Another template file, named box.ihtml, contains a block named row with three variable references {TITLE}, {NUM} and {BIGNUM}:
<!-- start box.ihtml -->
<table border=1 bgcolor="#cccccc" cellpadding=4 cellspacing=0>
<tr>
<td colspan=2><b>{TITLE}</b></td>
</tr>
<!-- BEGIN row -->
<tr>
<td>{NUM}</td>
<td>{BIGNUM}
</tr>
<!-- END row -->
</table>
<!-- end box.ihtml -->
The following php3 file demonstrates how to use these templates:
<?php
/* include the Template class */
include("template.inc");
/* create an instance of Template with desired parameters */
$t = new Template("/home/kris/www/test.koehntopp.de/pages/template", "keep");
/* $t->debug = 7; */ /* activate for full debugging */
/* define two variables from files */
$t->set_file(array(
"page" => "page.ihtml",
"box" => "box.ihtml"));
/* define a variable contained in another */
$t->set_block("box", "row", "rows");
/* define two variables manually */
$t->set_var(array("TITLE" => "Testseite",
"PAGETITLE" => "hugo"));
for ($i=1; $i<=3; $i++) {
$n = $i;
$nn = $i*10;
/* define values for NUM and BIGNUM */
$t->set_var(array("NUM" => $n, "BIGNUM" => $nn));
/* replace NUM and BIGNUM in row and
* append the result to rows */
$t->parse("rows", "row", true);
}
/* replace all variables in box, result in OUT,
* then replace all variables in page, result in OUT */
$t->parse("OUT", array("box", "page"));
/* print OUT */
$t->p("OUT");
?>
<hr>
Undefined variables in OUT:
<?php
print @implode(", ", $t->get_undefined("OUT"));
?>