A template consists of text and special labeled blocks and placeholders. The content of blocks can be re-used and parsed multiple times with different placeholder values.
A typical template
<html> <body> Userlist <table> <!-- BEGIN row --> <tr> <td>{USERNAME}</td> <td>{EMAIL}</td> </tr> <!-- END row --> </table> </body> </html>
Placeholders can be defined in templates and are filled from PHP code with content. The format of placeholder up to version (1.1.x) is
{[0-9A-Za-z_-]+}
Since version 1.2.x dots are allowed, too.
{[\.0-9A-Za-z_-]+}
This means, the name of the placeholder can consist of upper- and lowercase letters, underscores and hypens. The name must be placed between curly brackets without any spaces. Valid names are i.e.:
Valid names since version 1.2.x
Non-valid names are i.e.
The format of a block is
Since version 1.2.x dots are allowed in block definitions
The rules for the block name are the same like for placeholders. In contrast to placeholders the spaces in the block markup are required.
The nesting of blocks is permitted, but be careful while parsing. You have to set and parse the deepest inner block first and then set and parse from inner to outer.
In IT the whole template file itself is nested in a meta block called "__global__". Most block-related functions use this block name as default.
The template
The script
<?php
require_once "HTML/Template/IT.php";
$data = array
(
"0" => array("Stig", "Bakken"),
"1" => array("Martin", "Jansen"),
"2" => array("Alexander", "Merz")
);
$tpl = new HTML_Template_IT("./templates");
$tpl->loadTemplatefile("main.tpl.htm", true, true);
foreach($data as $name) {
foreach($name as $cell) {
// Assign data to the inner block
$tpl->setCurrentBlock("cell") ;
$tpl->setVariable("DATA", $cell) ;
$tpl->parseCurrentBlock("cell") ;
}
// parse outter block
$tpl->parse("row");
}
// print the output
$tpl->show();
?>
The output