This is the first post code here. this code is mainly used for PHP template engine technology research. Currently, only the compiled version is available. I hope you will provide more comments and optimization skills. It consists of three files. I don't know how to copy them as files. sorry! Index. php is a configuration file. you can see it. Index.html examples Templates. class. php base class A well-developed version with a cache will be released later, but I hope it will not be written to the cache. some friends or experts may advise that this template engine only needs to process compilation and cache, and other considerations will not be considered for the time being, of course, the regular expression replacement mode also requires f, w, and so on... I hope a friend can study me. Q: 76376931 The Copy_3_of_Templates.class.php file has been added to the cache mode. if you refresh the page again, the cache will not be generated. if you do not want to cache some pages in the project, you will use this class to add them gradually. I hope you can communicate with me!
- Header ('content-Type: text/html; charset = utf-8 ');
- Define ('root _ host', dirname (_ FILE __));
- Define ('HTML _ dir', ROOT_HOST. '/moban /');
- Define ('compiled _ dir', ROOT_HOST. '/data/COMPILED /');
- Define ('cache _ dir', ROOT_HOST. '/data/CACHE /');
- // Whether to enable the buffer
- Define ('New _ cache', false );
- // Determine whether the buffer zone is enabled
- NEW_CACHE? Ob_start (): null;
- // Introduce the template class
- Require ROOT_HOST. '/lib/Templates. class. php ';
- $ _ Moban = new Templates ();
- $ Array = array (a => 'Hi! ', B =>' I am not very good, but I miss you very much ', c =>' You are all at home, why do you still miss me? ');
- $ Xcvu = 'Hello, this is an xcvu ';
- $ Zmq = "hi ";
- $ Title = "this is a template engine custom method! ";
- $ Ling = "because a" function "???????????????? ";
- $ _ Moban-> assign ('ling', $ ling );
- $ _ Moban-> assign ('title', $ title );
- $ _ Moban-> assign ('zrabbit', $ zmq );
- $ _ Moban-> assign ('xcvu', $ xcvu );
- $ _ Moban-> assign ('ABC', 5> 4 );
- $ _ Moban-> assign ('array', $ array );
- $ _ Moban-> display('index.html ');
- ?>
-
- <! -- $ Title -->
- BBBasd doesn't know what to say, but wants to say something better.
-
-
-
1
-
2
-
-
- ........
-
-
- /* About: Richard. z
- * Site: http://www.zmq.cc
- * E_mail: code@zmq.cc
- * Date: 2013/01/02/17 :30
- **/
- Class Templates {
- Private $ _ CaChe;
- Private $ _ Compiled;
- Private $ _ HtmlFile;
- Private $ _ FileVar;
- Private $ _ KeyArr = array ();
- Public function _ construct (){
- If (! Is_dir (HTML_DIR) |! Is_dir (COMPILED_DIR) |! Is_dir (CACHE_DIR )){
- Exit ('your directory does not exist! ');
- }
- }
- Public function assign ($ _ var, $ _ value ){
- If (isset ($ _ var )&&! Empty ($ _ var )){
- $ This-> _ KeyArr [$ _ var] = $ _ value;
- } Else {
- Exit ('Please set your value! ');
- }
- }
- Public function display ($ _ File ){
- // Set Template variables
- $ This-> _ HtmlFile = HTML_DIR. $ _ File;
- // Set compilation
- $ This-> _ Compiled = COMPILED_DIR.md5 ($ _ File). $ _ File. '. php ';
- // Set cache
- $ This-> _ CaChe = cache_dir.md5(pai_file).pai_file.'.html ';
- // Determine whether the template exists
- If (! File_exists ($ this-> _ HtmlFile )){
- Exit ('Template file does not exist ');
- }
- // Value assignment and Read Judgment
- If (! $ This-> _ FileVar = file_get_contents ($ this-> _ HtmlFile )){
- Exit ('The template file read error! ');
- }
- // If edit Compiled File date <date HtmlFile
- If (! File_exists ($ this-> _ Compiled) | filemtime ($ this-> _ Compiled) <filemtime ($ this-> _ HtmlFile )){
- $ This-> Set_Comilled ();
- }
- // Include Compiled
- Include $ this-> _ Compiled;
- }
- // Public function
- Public function Set_Comilled (){
- $ This-> SetArr ();
- $ This-> SetInclude ();
- If (! File_put_contents ($ this-> _ Compiled, $ this-> _ FileVar )){
- Exit ('compiled files generated error! ');
- }
- }
- // Arr
- Private function SetArr (){
- $ _ Preaa = array (
- '/<\! -- \ S + \ $ ([\ w] +) \ s + \ --> /',
- '/<\! -- \ S + if \ s + \ $ ([\ w] +) \ s + \ --> /',
- '/<\! -- \ S + \/if \ s + \ --> /',
- '/<\! -- \ S + else \ s + \ --> /',
- '/<\! -- \ S + loop \ s + \ $ ([\ w] +) \ ([\ w] +), ([\ w] + )\) \ s + \ --> /',
- '/<\! -- \ S + \/loop \ s + \ --> /',
- '/<\! -- \ S + @ ([\ w] +) \ s + \ --> /',
- '/<\! -- \ S + \ # (. *) \ s + \ --> /');
- $ _ Prebb = array (
- ' _ KeyArr ["$1"];?> ',
- ' _ KeyArr ["$1"]) {?> ',
- ' ',
- ' ',
- ' _ KeyArr ["$1"] as \ $2 =>\$ $3) {?> ',
- ' ',
- ' ',
- ' ');
- $ This-> _ FileVar = preg_replace ($ _ preaa, $ _ prebb, $ this-> _ FileVar );
- If (preg_match ($ _ preaa [0], $ this-> _ FileVar )){
- $ This-> _ FileVar = $ this-> SetArr ($ this-> _ FileVar );
- }
- }
- // Include
- Private function SetInclude (){
- $ _ PreFile = '/<\! -- \ S + include \ s + file = \ "([\ w \. \-] +) \" \ s + \ --> /';
- If (preg_match ($ _ preFile, $ this-> _ FileVar, $ _ File )){
- If (! File_exists ($ _ File [1]) | empty ($ _ File )){
- Exit (You of Include File Error! ');
- }
- $ This-> _ FileVar = preg_replace ($ _ preFile ," ", $ This-> _ FileVar );
- }
- }
- }
- ?>
- /* About: Richard. z
- * Site: http://www.zmq.cc
- * E_mail: code@zmq.cc
- * Date: 2013/01/02/17 :30 | 2013/01/14/21 :35
- **/
- Class Templates {
- Private $ _ CaChe;
- Private $ _ Compiled;
- Private $ _ HtmlFile;
- Private $ _ FileVar;
- Private $ _ KeyArr = array ();
- Public function _ construct (){
- If (! Is_dir (HTML_DIR) |! Is_dir (COMPILED_DIR) |! Is_dir (CACHE_DIR )){
- Exit ('your directory does not exist! ');
- }
- }
- Public function assign ($ _ var, $ _ value ){
- If (isset ($ _ var )&&! Empty ($ _ var )){
- $ This-> _ KeyArr [$ _ var] = $ _ value;
- } Else {
- Exit ('Please set your value! ');
- }
- }
- Public function display ($ _ File ){
- // Set Template variables
- $ This-> _ HtmlFile = HTML_DIR. $ _ File;
- // Set compilation
- $ This-> _ Compiled = COMPILED_DIR.md5 ($ _ File). $ _ File. '. php ';
- // Set cache
- $ This-> _ CaChe = cache_dir.md5(pai_file).pai_file.'.html ';
- // Determine whether the template exists
- If (! File_exists ($ this-> _ HtmlFile )){
- Exit ('Template file does not exist ');
- }
- // Value assignment and Read Judgment
- If (! $ This-> _ FileVar = file_get_contents ($ this-> _ HtmlFile )){
- Exit ('The template file read error! ');
- }
- // If edit Compiled File date <date HtmlFile
- If (! File_exists ($ this-> _ Compiled) | filemtime ($ this-> _ Compiled) <filemtime ($ this-> _ HtmlFile )){
- $ This-> Set_Comilled ();
- }
- // Include Compiled
- Include $ this-> _ Compiled;
- $ This-> SetCaChe ();
- }
- // The setting cache file if you want to be generated again
- Private function SetCaChe (){
- If (! File_exists ($ this-> _ CaChe) | filemtime ($ this-> _ CaChe) <filemtime ($ this-> _ Compiled )){
- If (NEW_CACHE ){
- File_put_contents ($ this-> _ CaChe, ob_get_contents ());
- Ob_end_clean ();
- Include $ this-> _ CaChe;
- }
- }
- }
- // Public function
- Public function Set_Comilled (){
- $ This-> SetArr ();
- $ This-> SetInclude ();
- If (! File_put_contents ($ this-> _ Compiled, $ this-> _ FileVar )){
- Exit ('compiled files generated error! ');
- }
- }
- // Arr
- Private function SetArr (){
- $ _ Preaa = array (
- '/<\! -- \ S + \ $ ([\ w] +) \ s + \ --> /',
- '/<\! -- \ S + if \ s + \ $ ([\ w] +) \ s + \ --> /',
- '/<\! -- \ S + \/if \ s + \ --> /',
- '/<\! -- \ S + else \ s + \ --> /',
- '/<\! -- \ S + loop \ s + \ $ ([\ w] +) \ ([\ w] +), ([\ w] + )\) \ s + \ --> /',
- '/<\! -- \ S + \/loop \ s + \ --> /',
- '/<\! -- \ S + @ ([\ w] +) \ s + \ --> /',
- '/<\! -- \ S + \ # (. *) \ s + \ --> /');
- $ _ Prebb = array (
- ' _ KeyArr ["$1"];?> ',
- ' _ KeyArr ["$1"]) {?> ',
- ' ',
- ' ',
- ' _ KeyArr ["$1"] as \ $2 =>\$ $3) {?> ',
- ' ',
- ' ',
- ' ');
- $ This-> _ FileVar = preg_replace ($ _ preaa, $ _ prebb, $ this-> _ FileVar );
- If (preg_match ($ _ preaa [0], $ this-> _ FileVar )){
- $ This-> _ FileVar = $ this-> SetArr ($ this-> _ FileVar );
- }
- }
- // Include
- Private function SetInclude (){
- $ _ PreFile = '/<\! -- \ S + include \ s + file = \ "([\ w \. \-] +) \" \ s + \ --> /';
- If (preg_match ($ _ preFile, $ this-> _ FileVar, $ _ File )){
- If (! File_exists ($ _ File [1]) | empty ($ _ File )){
- Exit (You of Include File Error! ');
- }
- $ This-> _ FileVar = preg_replace ($ _ preFile ," ", $ This-> _ FileVar );
- }
- }
- }
- ?>
|