A simple template class-compiled (after the simplet class is enhanced), read a simple template class-compiled (after the simplet class is enhanced ), after the last release of the simplet class (http://bbs.phpchina.com/thread-85257-1-1.html), I found many bugs, such as not supporting multi-dimensional arrays, comments and no caching features, this time I put these features ">
After the last release of the simplet class (http://bbs.phpchina.com/thread-85257-1-1.html), I found many defects, such as not supporting multi-dimensional arrays, comments and no caching functions, this time I added these features !!!
If you are interested, I will give a tutorial next time to talk about some ideas and methods for writing this class !!! If it is supported, you can post it to support me!
PHP code:
Simplet. class. php
Class SimpleT {
Private $ t_vars;
Private $ templates_dir;
Private $ templates_c_dir;
Private $ cache;
Private $ cache_dir;
Public function _ construct (){
$ This-> templates_dir = './templates /';
$ This-> templates_c_dir = './templates_c /';
$ This-> cache = 0;
$ This-> cache_dir = './cache /';
}
Public function setDir ($ dir, $ type = 'template '){
If (is_dir ($ dir )){
If ($ type = 'template ')
$ This-> templates_dir = rtrim ($ dir ,'/').'/';
Elseif ($ type = 'Template _ c ')
$ This-> templates_c_dir = rtrim ($ dir ,'/').'/';
Elseif ($ type = 'cache ')
$ This-> cache_dir = rtrim ($ dir ,'/').'/';
Else
Return false;
Return true;
} Else {
Return false;
}
}
Public function cache ($ time ){
If (is_numeric ($ time )){
$ This-> cache = $ time;
Return true;
} Else {
Return false;
}
}
Public function assign ($ var, $ value = NULL ){
If (is_array ($ var )){
Foreach ($ var as $ key => $ val ){
$ This-> t_vars [$ key] = $ val;
}
} Else {
$ This-> t_vars [$ var] = $ value;
}
}
Private function comp ($ filename ){
Try {
If (! $ Fp = fopen ($ filename, 'r ')){
Throw new Exception ('Can not open'. $ filename );
}
$ Filesize = filesize ($ filename );
If ($ filesize <= 0 ){
Throw new Exception ('The file size must> 0 ');
}
$ Content = fread ($ fp, $ filesize );
Fclose ($ fp );
Unset ($ fp );
$ Content = preg_replace ("/<% = ([$ a-zA-Z0-9 _] {1,}) %> /"," ", $ Content );
$ Content = preg_replace ("/<% ([$ a-zA-Z0-9 _] {1,}) \. loop %> /"," \\$ $ Interval Val) {?> ", $ Content );
$ Content = preg_replace ("/<% ([\ $ a-zA-Z0-9 _] {1 ,})\. loop \ ([$ a-zA-Z0-9 _] {1,}) \) %> /"," ", $ Content );
$ Content = preg_replace ("/<% ([\ $ a-zA-Z0-9 _] {1 ,})\. loop \ ([\ $ a-zA-Z0-9 _] {1,}), ([\ $ a-zA-Z0-9 _] {1,}) \) %> /"," \\$ $3) {?> ", $ Content );
$ Content = preg_replace ("/<% ([$ a-zA-Z0-9 _] {1,}) \. key %> /"," ", $ Content );
$ Content = preg_replace ("/<% ([$ a-zA-Z0-9 _] {1,}) \. value %> /"," ", $ Content );
$ Content = preg_replace ("/<% ([\ $ a-zA-Z0-9 _] {1 ,})\\? %> /"," ", $ Content );
$ Content = preg_replace ("/<% end %> /"," ", $ Content );
$ Content = preg_replace ("/<% # common ###%> ([^ <% # end ##%>] {0 ,}) <% # end ##%> /"," ", $ Content );
If (preg_match_all ("/<% {([^ (}%>)] {1,}) }%>/", $ content, $ files )){
$ This-> comp ($ this-> templates_dir. $ files [1] [0]);
}
$ Content = preg_replace ("/<% {([^ (}%>)] {1,}) }%> /"," Templates_c_dir} simplet_comp _ $1. php';?> ", $ Content );
Echo $ content;
$ Fp = fopen ($ this-> templates_c_dir. 'let Let _ comp _ '. basename ($ filename).'. php', 'w ');
If (! Fwrite ($ fp, $ content )){
Throw new Exception ('Can not write content in the '. $ filename );
}
Fclose ($ fp );
} Catch (Exception $ e ){
Echo $ e-> getMessage ();
}
Return true;
}
Public function display ($ filename ){
$ Filename = $ this-> templates_dir. $ filename;
If (! File_exists ($ filename )){
Return false;
}
$ T_filename_c = $ this-> templates_c_dir. 'let Let _ comp _ '. basename ($ filename).'. php ';
If (! File_exists ($ t_filename_c) | filemtime ($ t_filename_c) <filemtime ($ filename )){
$ This-> comp ($ filename );
}
If ($ this-> cache> 0 ){
$ Cache_file = $ this-> cache_dir. basename ($ filename );
If (! File_exists ($ cache_file) | (time ()-filemtime ($ cache_file) >$ this-> cache ){
Ob_start ();
Foreach ($ this-> t_vars as $ key => $ val ){
$ Key = $ val;
}
Include ($ t_filename_c );
$ Content = ob_get_contents ();
Ob_end_clean ();
$ Fp = fopen ($ cache_file, 'w ');
Fwrite ($ fp, $ content );
Fclose ($ fp );
Echo $ content;
Unset ($ content );
} Else {
Include ($ cache_file );
}
} Else {
Foreach ($ this-> t_vars as $ key => $ val ){
$ Key = $ val;
}
Include ($ t_filename_c );
}
}
}
?>
PHP code:
Test. php
Require_once ('let Let. class. php ');
$ T = new SimpleT ();
$ T-> cache (10); // enable the cache function and set the expiration time to 10 seconds.
$ T-> assign ('arrays ', array ('hello', 'World ')));
$ T-> assign ('condition ', false );
$ T-> display ('index. php ');
?>
PHP code:
Index. php (the template file is stored in the templates/folder)
<% ## Common ##%>
Here is the comment
Below is a multi-dimensional array loop
<% ## End ##%>
<% Arrays. loop (value) %>
<% Value. loop (name) %>
<% = Name %>
<% End %>
<% End %>
<% ## Common ##%>
The following is conditional judgment.
<% ## End ##%>
<% Condition? %>
Condition is true
<% End %>
<% ## Common ##%>
The following is the inclusion File ()
<% ## End ##%>
<% {Filename. php} %>