Web Development With PHP FastTemplate - A Strict() Master
(Page 7 of 9 )
While the methods discussed above will suffice for most of your FastTemplate requirements, the class also comes with a bunch of ancillary capabilities.
The strict() method is used to display an error if FastTemplate finds template variables without any values assigned to them; these undefined variables will also appear as is in the final output.
<?
// strict error checking
$obj->strict();
?>
The opposite of this is the no_strict() method, which replaces these
unassigned variables with empty strings.
<?
// turn off error checking
$obj->no_strict();
?>
The fetch() method returns the raw data which results from a parse()
operation. Consider the following:
<?
// parse templates
$obj->parse(RESULT, "list");
// print
echo $obj->fetch("RESULT");
?>
The clear() method is used to clear variables.
<?
// parse templates$obj->parse(RESULT, "list");// clear$obj->clear("RESULT");// prints nothing$obj->FastPrint("RESULT");?>
The get_assigned() method is used to obtain the value of any FastTemplate variable.
<?
$obj->assign("EMAIL", "jdoe@somewhere.com");
// returns "jdoe@somewhere.com"
echo $obj->get_assigned("EMAIL");
?>
And finally, the utime() function comes in handy when you need to measure
script execution time.
<?
// list.php - item list
// include class file
include("class.FastTemplate.php3");
// instantiate new object
$obj = new FastTemplate("./tmpl/");
// get start time
$begin = $obj->utime();
// assign names for template files
$obj->define(array(
"list" => "list.tpl",
"list_item" => "list_item.tpl"
));
// get item list
// assume it looks like this..
$items = array("vanilla", "pineapple", "strawberry", "chocolate chip",
"peach", "banana", "grape");
// build LISTCONTENT variable by concatenation of multiple instances of
"list_item" template
for ($x=0; $x<sizeof($items); $x++)
{
$obj->assign("ITEM", $items[$x]);
$obj->parse(LISTCONTENT, ".list_item");
}
// parse templates
$obj->parse(RESULT, "list");
// and print
$obj->FastPrint(RESULT);
// get end time
$end = $obj->utime();
// print script execution time
echo "Done in " . sprintf("%01.3f ", ($end - $begin)) ." seconds.";
?>
Next: Musical Chairs >>
More PHP Articles
More By icarus, (c) Melonfire