Compile PHP template engine roughly implements the process template engine
TemplateDir = $ templateDir; $ this-> templateCompileDir = $ templateComplieDir;}/*** display template * @ param string $ fileName template file name */public function display ($ fileName) {$ this-> fileName = $ fileName; if (file_exists ($ this-> templateDir. '/'. $ this-> fileName) {$ compileFileName = $ this-> templateCompileDir. '/'. $ this-> file_safe_name (). '. php'; if (! File_exists ($ compileFileName) | filemtime ($ compileFileName) <filemtime ($ this-> templateDir. '/'. $ this-> fileName) {$ this-> del_old_file (); $ this-> compile ();} extract ($ this-> templateVar); include $ compileFileName ;} else {$ this-> error ('sorry, the template file '. $ this-> fileName. 'Does not exist !! ') ;}}/*** Get the compilation file name */private function get_compile_file () {$ compileFile = explode ('. ', $ this-> fileName); unset ($ compileFile [count ($ compileFile)-1]); return implode ('. ', $ compileFile);}/*** compile */private function compile () {$ fileHandle = @ fopen ($ this-> templateDir. '/'. $ this-> fileName, 'r'); while (! Feof ($ fileHandle) {$ fileContent = fread ($ fileHandle, 1024);} fclose ($ fileHandle); $ fileContent = $ this-> template_replace ($ fileContent ); // $ compileFile = $ this-> get_compile_file ($ fileName); $ fileHandle = @ fopen ($ this-> templateCompileDir. '/'. $ this-> file_safe_name (). '. php ', 'w'); if ($ fileHandle) {fwrite ($ fileHandle, $ fileContent); fclose ($ fileHandle );} else {$ this-> error ('sorry, Compile dir can not wri Te! ') ;}}/*** Template passed value * @ param string $ variable name used in the valueName template * @ param $ value variable value */public function assign ($ valueName, $ value) {$ this-> templateVar [$ valueName] = $ value ;} /*** template regular replacement * @ param string $ content * @ return string content after replacement */private function template_replace ($ content) {$ orginArray = array ('/
/S ','/
/S ','/
(. + ?)
/S ','/
/S ','/
/S ','/
/S ','/
/S ','/
/S ','/\ {P :(. + ?) \:}/S', '/\ {C :( \ w +) \}/S','/\ {I :(. + ?) \}/S', '/\ {F :(. + ?) \}/S', '/\ {EF :(. + ?) \}/S ','/\ {([a-zA-Z0-9 _ \ [\] \ '\ "\ $ \. \ x7f-\ xff] +) \}/S',); $ changeArray = array ('
','
$3) {$ countLoop ++;?> ','
$1
','
','
','
','
','
','
','
','
TemplateDir. '/$1 ";?> ','
','
','
',); Return $ repContent = preg_replace ($ orginArray, $ changeArray, $ content);}/*** delete old file */private function del_old_file () {$ compileFile = $ this-> get_compile_file ($ this-> fileName); $ files = glob ($ this-> templateCompileDir. '/'. $ compileFile. '*. php '); // print_r ($ files); if ($ files) {@ unlink ($ files [0]);} /*** secure solution for compiling file names * @ return string returns the compilation file name */private function file_safe_name () {$ compileFile = $ this-> get_compile_file ($ this-> fileName ); return $ compileFile. filemtime ($ this-> templateDir. '/'. $ this-> fileName);}/*** error output function * @ param string $ content error output information */private function error ($ content) {$ stringHtml =''; $ StringHtml. = 'Error information:
'; $ StringHtml. = ''; $ stringHtml. = $ content; $ stringHtml. =''; $ stringHtml. ='
'; Exit ($ stringHtml) ;}}?>