/**
The class name is Smarty, which is a custom template engine
Load the template file through this class object and parse it, and output the parsed result
*/
class Smarty {
public $template_dir = 'templates'; //Define the directory where template files are stored
public $compile_dir = 'templates_c'; //Define the file storage directory after combining through the template engine
public $left_delimiter = '<{'; ; ; ($tpl_var, $value = null) {
if ($tpl_var != '')
$this->tpl_vars[$tpl_var] = $value;
}
/**
The values assigned in PHP will be saved to the member attribute $tpl_vars, which is used to replace the corresponding variables in the board @paramstring
$tpl_varrequires a string parameter as an associative array The subscript must correspond to the variable name in the template @parammixed$value
requires a scalar type value to be assigned to the value of the variable in the template
*/
function display($fileName) {
/* Find the template file in the specified directory*/
$tplFile = $this->template_dir.'/'.$fileName; /* If the template file that needs to be processed does not exist, exit and report an error */
if(!file_exists($tplFile)) { ");
}
/* Get the combined template file, the contents in the file have been replaced*/
$comFileName = $this->compile_dir."/com_".$fileName. '.php';
/* Determine whether the replaced file exists or if it exists but has been changed, you need to recreate it */
if(!file_exists($comFileName) || filemtime($comFileName) < filemtime( $tplFile)) {
/* Call the internal replacement template method*/
$repContent = $this->tpl_replace(file_get_contents($tplFile));
/* Save the script file combined by the system +/
} */**
Private method used internally, using regular expressions to replace the statements in the template file '<{ }>' with the corresponding values or PHP code
@paramstring$content
provided The entire content string read from the template file
@return
$repContent
Return the replaced string
*/private function tpl_replace ($ Content) {/*In the left and right boundary symbols, there are special symbols that affect the regular symbols, for example, & lt; {{{ }>Escape<{ }> */
$left = preg_quote($this->left_delimiter, '/');$right = preg_quote($this->right_delimiter, '/ ');/* Pattern array of regular expressions matching various identifiers in the template*/
$pattern = array(
/* Matching variables in the template, for example, "<{ $var }>" */
'/'.$left.'s*$([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)s*'.$right. '/i',
/* Match the if identifier in the template, such as "<{ if $col == "sex" }> <{ /if }>" */
'/'.$left .'s*ifs*(.+?)s*'.$right.'(.+?)'.$left.'s*/ifs*'.$right.'/ies',
/* Matches elseif identifiers, such as "<{ elseif $col == "sex" }>" */
'/'.$left.'s*elses*ifs*(.+?)s*'. $right.'/ies',
/* matches else identifiers, such as "<{ else }>" */'/'.$left.'s*elses*'.$right. '/is',
/* is used to match the loop identifier in the template and to traverse the values in the array, such as "<{ loop $arrs $value }> <{ /loop}>" */
'/'.$left.'s*loops+$(S+)s+$([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)s*'.$ right.'(.+?)'.$left.'s*/loops*'.$right.'/is',
/* is used to traverse the keys and values in the array, such as "<{ loop $arrs $key => $value }> <{ /loop}>" */
'/'.$left.'s*loops+$(S+)s+$([a-zA-Z_x7f -xff][a-zA-Z0-9_x7f-xff]*)s*=>s*$(S+)s*'.$right.'(.+?)'.$left.'s*/loop s*'.$right.'/is',
/* matches include identifiers, for example, '<{ include "header.html" }>' */
'/'.$left. 's*includes+["']?(.+?)["']?s*'.$right.'/ie'
);
/* Replace using from template String array matched by the regular expression */
$replacement = array(
/* Replace variables in the templatetpl_vars["var"]; */ 'tpl_vars["${1}"]; ?>',
/* Replace the if string in the template */
'$this->stripvtags('','${2 }')',
/* Replace elseif string */
'$this->stripvtags('',"")',
/* Replace the string of else */
'',
/* The following two items are used to replace the loop identifier in the template with the foreach format*/'tpl_vars["${1} "] as $this->tpl_vars["${2}"]) { ?>${3}',
'tpl_vars["${1}"] as $this->tpl_vars["${2}"] => $this- >tpl_vars["${3}"]) { ?>${4}',
/*Replace include string*/
'file_get_contents($this ->template_dir."/${1}")'
);
/* Use regular replacement function */
$repContent = preg_replace($pattern, $replacement, $content);
/* If there are still identifiers to be replaced, recursively call yourself to replace again*/
if(preg_match('/'.$left.'([^('.$right.')]{ 1,})'.$right.'/', $repContent)) {
$repContent = $this->tpl_replace($repContent);
}
/* Return the replaced string */
return $repContent;
) var_pattern = '/s*$([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)s*/is';
/* Replace variables with values*/
$expr = preg_replace($var_pattern, '$this->tpl_vars["${1}"]', $expr);
/* Replace the escaped quotes in the start tag*/$ expr = str_replace("\"", """, $expr);/* Replace quotation marks in the statement body and closing tag*/
$statement = str_replace("\"", """, $statement); /* Connect the processed conditional statements and return */
return $expr.$statement;
}}
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the PHP regular expression-imitating smarty, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.