I wrote a template engine file.
The code is as follows:
/**
* The OOP template engine developed based on the Discuz template engine can cache templates and generate hash md5 values. The hash value is used to determine whether the template is modified. if the template is modified, the cached file is regenerated. if the template is not modified, the cached file is called directly.
* Version: 1.0.0.1 beta
*/
Class mmtp {
Var $ left_tags = "{";
Var $ right_tags = "}";
Var $ tp_suffix = ". html ";
Var $ cache_suffix = ". tpl ";
Var $ tp_dir = "./";
Var $ cache_dir = "./";
/**
* Number of times nested loops are allowed. the default value is 5.
*
* @ Var unknown_type
*/
Var $ nest = 5;
/**
* Template path
*
* @ Param unknown_type $ tp_dir
* @ Return mmtp
*/
Function _ setdir ($ tp_dir ){
If (file_exists ($ tp_dir )){
$ This-> tp_dir = $ tp_dir;
} Else {
$ This-> error ("The template path does not exist ");
}
}
/**
* Set cache Directory
*
* @ Param unknown_type $ cache_dir
*/
Function _ setcdir ($ cache_dir ){
If (file_exists ($ cache_dir )){
$ This-> cache_dir = $ cache_dir;
} Else {
$ This-> error ("the cache path does not exist ");
}
}
/**
* Output error message
*
* @ Param unknown_type $ msg
*/
Function error ($ msg ){
Print"
". $ Msg ."
";
}
/**
* Parsing template
*
* @ Param unknown_type $ file
*/
Function tp ($ file ){
$ Tp_path = $ this-> tp_dir. $ file. $ this-> tp_suffix;
$ Fp = fopen ($ tp_path, "rb ");
If (! $ This-> file_test ($ tp_path, "r ")&&! $ This-> match_hash ($ file )){
$ Template = $ this-> file_read ($ tp_path );
$ Var_regexp = "(\ $ [a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *) (\ [a-zA-Z0-9 _\-\. \ "\ '\ [\] \ $ \ x7f-\ xff] + \]) *";
$ Const_regexp = "([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *)";
$ Template = preg_replace ("/([\ n \ r] +) \ t +/s", "\ 1", $ template );
$ Template = preg_replace ("/\ <\! \-\ {(. + ?) \}\-\>/S "," {\\ 1} ", $ template );
$ Template = preg_replace ("/\ {lang \ s + (. + ?) \}/Ies "," your agevar ('\ 1') ", $ template );
$ Template = str_replace ("{LF }"," ", $ Template );
$ Template = preg_replace ("/\ {(\ $[a-zA-Z0-9 _ \ [\] \ '\" \ $ \. \ x7f-\ xff] +) \}/s "," ", $ Template );
$ Template = preg_replace ("/$ var_regexp/es", "\ $ this-> addquote (' ') ", $ Template );
$ Template = preg_replace ("/\ <\? \=\< \? \=$ Var_regexp \? \> \? \>/Es "," \ $ this-> addquote (' ') ", $ Template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {template \ s + ([a-z0-9 _] +) \} [\ n \ r \ t] */is "," \ n Cache_dir. "\ 1". $ this-> cache_suffix. "');?> \ N ", $ template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {template \ s + (. + ?) \} [\ N \ r \ t] */is "," \ n Cache_dir. "\ 1". $ this-> cache_suffix. "');?> \ N ", $ template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {eval \ s + (. + ?) \} [\ N \ r \ t] */ies "," \ $ this-> stripvtags ('\ n \ N', '')", $ template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {echo \ s + (. + ?) \} [\ N \ r \ t] */ies "," \ $ this-> stripvtags ('\ n \ N', '')", $ template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {elseif \ s + (. + ?) \} [\ N \ r \ t] */ies "," \ $ this-> stripvtags ('\ n \ N', '')", $ template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {else \} [\ n \ r \ t] */is", "\ n \ N ", $ template );
For ($ I = 0; $ I <$ this-> nest; $ I ++ ){
$ Template = preg_replace ("/[\ n \ r \ t] * \ {loop \ s + (\ S +) \} [\ n \ r] * (. + ?) [\ N \ r] * \ {\/loop \} [\ n \ r \ t] */ies "," \ $ this-> str1_tags ('\ n ',' \ N \ 3 \ n \ N') ", $ template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {loop \ s + (\ S +) \ s + (\ S +) \} [\ n \ r \ t] * (. + ?) [\ N \ r \ t] * \ {\/loop \} [\ n \ r \ t] */ies "," \ $ this-> str1_tags ('\ n \ 3) {?> ',' \ N \ 4 \ n \ N') ", $ template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {if \ s + (. + ?) \} [\ N \ r] * (. + ?) [\ N \ r] * \ {\/if \} [\ n \ r \ t] */ies "," \ $ this-> str1_tags ('\ n ',' \ N \ 2 \ n \ N') ", $ template );
}
$ Template = preg_replace ("// \{$ const_regexp \}/s "," ", $ Template );
$ Template = preg_replace ("/\? \> [\ N \ r] * \ <\? /S "," ", $ template );
$ Hash = $ this-> file_hash ($ tp_path );
$ Head_hash =" ";
$ Foot_time =" ";
$ This-> file_write ($ this-> cache_dir. $ file. ". tpl", $ head_hash. $ template. $ foot_time );
}
}
/**
* Check whether the file exists and has the read permission
*
* @ Param unknown_type $ path
*/
Function file_test ($ path, $ method ){
If (! File_exists ($ path) |! Fopen ($ path, $ method )){
$ This-> error ("The template file does not exist, or you do not have operation permissions ");
Return false;
}
}
/**
* Reading file content
*
* @ Param unknown_type $ path
* @ Return unknown
*/
Function file_read ($ path, $ length = 0 ){
If (! $ This-> file_test ($ path, "r + ")){
$ Fp = @ fopen ($ path, "r + ");
If ($ length = 0 ){
$ Contents = @ fread ($ fp, filesize ($ path ));
} Else {
$ Contents = @ fread ($ fp, $ length );
}
Fclose ($ fp );
Return $ contents;
}
}
/**
* Write file content
*
* @ Param unknown_type $ path
* @ Param unknown_type $ puts
*/
Function file_write ($ path, $ puts ){
If (! $ This-> file_test ($ path, "w + ")){
$ Fp = @ fopen ($ path, "w + ");
@ Fwrite ($ fp, $ puts );
Fclose ($ fp );
}
}
/**
* Calculate the file hash.
*
* @ Param unknown_type $ path
* @ Return unknown
*/
Function file_hash ($ path ){
Return md5_file ($ path );
}
/**
* Compare the hash values of template files and cached files
*
* @ Param unknown_type $ file
* @ Return unknown
*/
Function match_hash ($ file ){
$ Read_hash = $ this-> file_read ($ this-> cache_dir. $ file. $ this-> cache_suffix, 46 );
$ Html_hash = $ this-> file_hash ($ this-> tp_dir. $ file. $ this-> tp_suffix );
If (preg_match ("/". $ html_hash. "/I", $ read_hash )){
Return true;
}
}
Function addquote ($ var ){
Return str_replace ("\" "," \ "", preg_replace ("/\ [([a-zA-Z0-9 _\-\. \ x7f-\ xff] +) \]/s "," ['\ 1'] ", $ var ));
}
Function transamp ($ str ){
$ Str = str_replace ('&', '&', $ str );
$ Str = str_replace ('&', '&', $ str );
$ Str = str_replace ('\ "', '"', $ str );
Return $ str;
}
Function stripvtags ($ expr, $ statement ){
$ Expr = str_replace ("\\\" "," \ "", preg_replace ("/\< \? \= (\\\$. + ?) \? \>/S "," \ 1 ", $ expr ));
$ Statement = str_replace ("\\\" "," \ "", $ statement );
Return $ expr. $ statement;
}
}
$ Tp = new mmtp ();
$ Tp->__ setdir ("./");
$ Tp->__ setcdir ("./cache /");
$ Tp-> tp ("index1 ");
$ _ GET [it] = sdhkadajksdhajdhkajsdhjkasdjkasdhasjdhkjsadhk;
$ Name = 2;
$ Head = "Welcome to the MoMo template engine ";
Include ("./cache/index1.tpl ");
?>
Template index.html
The code is as follows:
{$ Head}
Template index1.html
The code is as follows:
{Template index}
{If $ name = 1}
Hi!
{Else}
Thank you.
{/If}
This template was written in silence this afternoon. it was written in a rush and may have vulnerabilities. this version is only a test version. I will gradually improve it later and start it first as a forward looking.