The Html_template_it in pear is a simple and easy-to-use class.
Create a template file ****************
The "symbol" naming convention is the following regular expression:
{[0-9a-za-z_-]+}
The "block" format is the following, with the block name naming convention and the same as "symbol":
... Block content ...
Blocks allow nesting, you must first set up and analyze the inner blocks, and then set and analyze the blocks on the previous layer.
Main methods ***********
constructor function:
void Integratedtemplate::integratedtemplate ([String $root = ""])
$root = Touch Plate root directory
You can also use
void Integratedtemplate::setroot (String $root)
method to set the template directory; The method cannot be called statically.
---------------------------------------------------------
To load a template:
Boolean Integratedtemplate::loadtemplatefile (String $filename, Boolean [$removeUnknownVariables = TRUE], Boolean [$ Removeemptyblocks = TRUE])
$filename = Template file name
$removeUnknownVariables = Sets whether the unknown variable is removed
$removeEmptyBlocks = set whether to remove empty blocks
The success returns True, and the failure returns false.
The method cannot be called statically
-------------------------------------------------------
Specify the block currently being processed:
Boolean Integratedtemplate::setcurrentblock ([String $block = "__global"])
$block = block Name
Successful return True, Failure returns a Pear_error object containing an error
Describe. The method cannot be called statically.
-------------------------------------------------------
Replace the "symbol" in the block currently being processed with a variable:
void integratedtemplate::setvariable (mixed $placeholder [, Mixed $variable = ""])
$placeholder = The symbol name to replace, if $variable is an array,
Then use the symbol name as the index of this array and use the cable
Data substitution symbol for an array element that is cited
$variable = variable name or array name
The function cannot be called statically
--------------------------------------------------------
Parse the block currently being processed:
void Integratedtemplate::p arsecurrentblock ()
Failure will return a Pear_error object containing an error
Describe. The method cannot be called statically.
---------------------------------------------------------
Processing completion output:
void Integratedtemplate::show ([string $block])
$block = The block to be returned
If $block is not set, the full template will be returned
Simple example (sample from Pear's online manual) * * * * * *
Usage Example
Example 25-1. main.tpl.htm template file, under the "./templates" Directory
Example 25-2. PHP script
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");
}
Assign data and the inner block to the
Outer block
$tpl->setcurrentblock ("Row");
$tpl->parsecurrentblock ("Row");
}
Output
$tpl->show ();
?>
Example 25-3. Output
Stig
|
Bakken
|
Martin
|
Jansen
|
Alexander
|
Merz
|
*******************************************************
Html_template_it also has a ITX class that can perform more complex functions,
Inherit from it class; ITX. The original code of the PHP file contains the method description for this class.
For a detailed description of the other methods of Html_template_it, please refer to the Pear manual at
http://pear.php.net/manual/en/package.html.html-template-it.php
--Hope you can make good use of pear this library to write more and better PHP programs! --