After using the SMARTY,TP, also want to understand how its template technology is implemented, so write a simple template class, is basically read template file-> Replace template file content-> Save or static
tpl.class.php Main Analysis
Assign method implementation
/**
* Template Assignment Operation
* @param mixed $TPL _var If it is a string, it is an array index, and if it is an array, the value of the loop assignment
* @param mixed $tpl _value when $tpl_ Value when Var is string, default is null
/public
function Assign ($tpl _var, $tpl _value=null) {
if (Is_array ($tpl _var) && count ($tpl _var) > 0) {
foreach ($tpl _var as $k => $v) {
$this->tpl_vars[$k] = $v;
}
} ElseIf ($tpl _var) {
$this->tpl_vars[$tpl _var] = $tpl _value;
}
}
Fetch method implementation
/**
* Generate compilation file
* @param string $tplFile template path
* @param string $comFile compile path
* @return string
/
Priv Ate function Fetch ($tplFile, $comFile) {
//Determine whether the compiled file needs to be rebuilt (whether the compiled file exists or the template file is modified more than the compilation file modified)
if (!file_exists) ($ Comfile) | | Filemtime ($tplFile) > Filemtime ($comFile)) {
//compilation, where you can also use Ob_start () for static
$content = $this-> Tplreplace (file_get_contents ($tplFile));
File_put_contents ($comFile, $content);
}
Simple Compilation Method: regular substitution by rule
/**
* Compile
the file * @param string $content The content to be compiled
* @return string
/Private Function Tplreplace ($content {
///Escape the left-right delimiter regular expression character
$left = Preg_quote ($this->left_delimiter, '/');
$right = Preg_quote ($this->right_delimiter, '/');
Simple analog compilation variable
$pattern = Array (
//For example {$test}
'/'. $left. \$ ([a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*) '. $right. ' /I '
);
$replace = Array (
' <?php echo $this->tpl_vars[\ ' ${1}\ ';?> '
);
Regular process return
preg_replace ($pattern, $replace, $content);
display = Fetch+echo
/**
* Output
* @param string $fileName template file name
*
/Public Function display ($fileName) {
//template path
$ Tplfile = $this->template_dir. '/'. $fileName;
Determine if the template exists
if (!file_exists ($tplFile)) {
$this->errormessage = ' template file does not exist ';
return false;
}
The compiled file
$comFile = $this->compile_dir. MD5 ($fileName). PHP ';
$this->fetch ($tplFile, $comFile);
Include $comFile;
}
Other properties
Template file storage location
private $template _dir = ' templates ';
Compile file storage location
private $compile _dir = ' compiles ';
Left delimiter
private $left _delimiter = ' {';
Right delimiter
private $right _delimiter = '} ';
Internal temporary variable, store user assigned
private $TPL _vars = Array ();
Error message
private $errorMessage = ';
/**
* Modify the value of the class attribute
* @param array $configs related properties and values that need to be modified
* @return bool/public
function setconfigs (Array $configs) {
if (count ($configs) > 0) {
foreach ($configs as $k => $v) {
if (isset ($this-> $k))
$this- > $k = $v;
}
return true;
}
return false;
}
Test
Template file testtpl.html
Output: Waited:100:this is a demo
Build compilation file: 972fa4d270e295005c36c1dbc7e6a56c.php
The above is the entire content of this article, I hope to help you learn.