General implementation process of a compiled PHP template engine

Source: Internet
Author: User

JTemplate. class. php

001
<? Php
002
/**
003
* <A href = "http://my.oschina.net/arthor" target = "_ blank" rel = "nofollow"> @ author </a> Jiawei
004
* @ Completed in
005
*/
006
Class JTemplate {
007
// The variables passed in through the assign function temporarily store Arrays
008
Private $ templateVar = array ();
009
// Template directory
010
Private $ templateDir = '';
011
// Compile the Directory
012
Private $ templateCompileDir = '';
013

014
Private $ fileName = '';
015
/**
016
* Constructor
017
* @ Param string $ templateDir template directory
018
* @ Param string $ templateComplieDir template compilation directory
019
*/
020
Public function _ construct ($ templateDir, $ templateComplieDir ){
021
$ This-> templateDir = $ templateDir;
022
$ This-> templateCompileDir = $ templateComplieDir;
023
}
024
/**
025
* Display template
026
* @ Param string $ fileName template file name
027
*/
028
Public function display ($ fileName ){
029
$ This-> fileName = $ fileName;
030
If (file_exists ($ this-> templateDir. '/'. $ this-> fileName )){
031
$ CompileFileName = $ this-> templateCompileDir. '/'. $ this-> file_safe_name (). '. php ';
032
If (! File_exists ($ compileFileName) | filemtime ($ compileFileName) <filemtime ($ this-> templateDir. '/'. $ this-> fileName )){
033
$ This-> del_old_file ();
034
$ This-> compile ();
035
}
036
Extract ($ this-> templateVar );
037
Include $ compileFileName;
038
} Else {
039
$ This-> error ('Sorry, the template file'. $ this-> fileName. 'does not exist !! ');
040
}
041
}
042
/**
043
* Get the compilation file name
044
*/
045
Private function get_compile_file (){
046
$ CompileFile = explode ('.', $ this-> fileName );
047
Unset ($ compileFile [count ($ compileFile)-1]);
048
Return implode ('.', $ compileFile );
049
}
050
/**
051
* Compile
052
*/
053
Private function compile (){
054
$ FileHandle = @ fopen ($ this-> templateDir. '/'. $ this-> fileName, 'R ');
055
While (! Feof ($ fileHandle )){
056
$ FileContent = fread ($ fileHandle, 1024 );
057
}
058
Fclose ($ fileHandle );
059
$ FileContent = $ this-> template_replace ($ fileContent );
060
// $ CompileFile = $ this-> get_compile_file ($ fileName );
061
$ FileHandle = @ fopen ($ this-> templateCompileDir. '/'. $ this-> file_safe_name (). '. php', 'w ');
062
If ($ fileHandle ){
063
Fwrite ($ fileHandle, $ fileContent );
064
Fclose ($ fileHandle );
065
} Else {
066
$ This-> error ('Sorry, Compile dir can not write! ');
067
}
068
}
069
/**
070
* Pass the template value
071
* @ Param string $ variable name used in the valueName Template
072
* @ Param $ value variable value
073
*/
074
Public function assign ($ valueName, $ value ){
075
$ This-> templateVar [$ valueName] = $ value;
076
}
077

078
/**
079
* Template Regular Expression replacement
080
* @ Param string $ content replace content
081
* <A href = "http://my.oschina.net/u/556800" target = "_ blank" rel = "nofollow"> @ return </a> string replaced content
082
*/
083
Private function template_replace ($ content ){
084
$ OrginArray = array (
085
'/<! -- Loop \ s + \ $ (\ w +) -->/s ',
086
'/<! -- Loop \ s + \ $ (\ w +) -->/s ',
087
'/<! -- Elseloop --> (. + ?) <! -- Endloop -->/s ',
088
'/<! -- Endloop -->/s ',
089
'/<! -- If \ s + \ (. + ?) \) -->/S ',
090
'/<! -- Endif -->/s ',
091
'/<! -- Elseif \ s + \ (. + ?) \) -->/S ',
092
'/<! -- Else -->/s ',
093
'/\ {P :(. + ?) \:}/S ',
094
'/\ {C :( \ w +) \}/s ',
095
'/\ {I :(. + ?) \}/S ',
096
'/\ {F :(. + ?) \}/S ',
097
'/\ {EF :(. + ?) \}/S ',
098
'/\ {([A-zA-Z0-9 _ \ [\] \' \ "\ $ \. \ x7f-\ xff] +) \}/s ',
099
);
100

101
$ ChangeArray = array (
102
'<? Php if (! Empty ($1) & is_array ($1) {$ countLoop = 1; foreach ($1 as $2) {$ countLoop ++;?> ',
103
'<? Php if (! Empty ($1) & is_array ($1) {$ countLoop = 1; foreach ($1 as $2 =>$ $3) {$ countLoop ++;?> ',
104
'<? Php} if (! Empty ($ countLoop) $ countLoop --;} else {?> $1 <? Php }?> ',
105
'<? Php} if (! Empty ($ countLoop) $ countLoop -- ;}?> ',
106
'<? Php if ($1) {?> ',
107
'<? Php }?> ',
108
'<? Php} elseif ($1) {?> ',
109
'<? Php} else {?> ',
110
'<? Php $1?> ',
111
'<? Php echo $1;?> ',
112
'<? Php include_once "'. $ this-> templateDir.'/$1";?> ',
113
'<? Php $1;?> ',
114
'<? Php echo $1;?> ',
115
'<? Php echo $1;?> ',
116
);
117
Return $ repContent = preg_replace ($ orginArray, $ changeArray, $ content );
118
}
119
/**
120
* Delete an old file
121
*/
122
Private function del_old_file (){
123
$ CompileFile = $ this-> get_compile_file ($ this-> fileName );
124
$ Files = glob ($ this-> templateCompileDir. '/'. $ compileFile. '*. php ');
125
// Print_r ($ files );
126
If ($ files ){
127
@ Unlink ($ files [0]);
128
}
129
}
130
/**
131
* Secure solution for compiling file names
132
* <A href = "http://my.oschina.net/u/556800" target = "_ blank" rel = "nofollow"> @ return </a> string returns the compiled file name
133
*/
134
Private function file_safe_name (){
135
$ CompileFile = $ this-> get_compile_file ($ this-> fileName );
136
Return $ compileFile. filemtime ($ this-> templateDir. '/'. $ this-> fileName );
137
}
138

139
/**
140
* Error output function
141
* @ Param string $ content error output information
142
*/
143
Private function error ($ content ){
144
$ StringHtml = '<div style = "width: 780px; height: auto; padding: 10px; border: 1px solid # CCC; margin: 0 auto;"> ';
145
$ StringHtml. = 'error information: <br/> ';
146
$ StringHtml. = '<font color = "red"> ';
147
$ StringHtml. = $ content;
148
$ StringHtml. = '</font> ';
149
$ StringHtml. = '</div> ';
150
Exit ($ stringHtml );
151
}
152
}
153
?>
Index. php

View sourceprint?
01
<? Php
02
/**
03
* @ Author Jiawei
04
*/
05
Include_once 'jtemplate/JTemplate. class. php ';
06
$ Template = new JTemplate ('template', 'compile ');
07
$ A = array ('A', 'B', 'C', 'D ');
08
Define ('_ CORE _', 'aaa ');
09
$ Template-> assign ('A', $ );
10
$ Template-> display('index.html ');
11
?>
Author: Yuan Jiawei

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.