General implementation process of the compiled PHP template engine-PHP source code

Source: Internet
Author: User
Tags php template
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) ;}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.