| SimpleTemplate | ||
|---|---|---|
| Prev | ||
Now you know how to make a template, you need to know how to use it from PHP.
require 'lib/SimpleTemplate.php';
$template = new SimpleTemplate('templateFile.tpl'); |
Sets the current block to blockPath. The blockPath's similar to a XML XPath expression, with the root block called '/' and each subblock adding to it in the format '/block1/block2/block3'.
Sets the block to the one set before the previous call to setBlock().
Parses the currently selected block and adds any addBlock()'d subblocks. Variable modifiers are applied at this point. Any set variables are cleared.
Adds 'content' to the current block without any form of processing.
Returns the parsed content of the current block and clears it so it will NOT be used in any addBlock() calls in any superblocks. Use along with addBlockRaw() to join different templates together.
As get(), only does not clear. Useful for caching final output.
General purpose variable setting method. vars may be an array (with array indexes as variable names), object (with attribute names as variable names), or a string giving the variable name the second argument should be assigned to.
High performance variable setting method for arrays and objects only. The argument is passed by reference, so you should be careful to avoid changing it unless you're really sure you want to.
High performance variable setting method for assigning long strings to variables.
Appends value to the variable 'name'.
$t = new SimpleTemplate('news.tpl');
$t->setBlock('/sideBox');
foreach ($sideBoxes AS $sideBox)
{
$t->setVar('Title',$sideBox['title']);
$t->setVarByRef('Body',$sideBox['body']);
$t->addBlock();
}
$t->setBlock('/article');
foreach($articles AS $article)
{
$t->setVarsByRef($article);
$t->addBlock()
}
$t->setBlock('/');
$t->setVar('Title' => 'SimpleTemplate Example');
$t->addBlock();
print $t->get(); |