Copy codeThe Code is as follows:
<? Php
/* Template. class. php
@ Kangsheng Weibo Template Extraction class thinks this template is easy to use and takes some time to be independent. By raysun
@ Check whether the ctt template is similar to the phpcms template ?? ^_^ Hey !!!
@ Micro blog http://weibo.com/lrjxgl
@ Good stuff everyone can share with us all the difficulties to extract. If you have any questions, please come up with me.
@ The default template file is. htm.
$ Tpl = new template ('skin', "default ");
$ Tpl-> objdir = 'tpp ';
$ Tpl-> rewrite = true; // The server must support enabling rewrite.
$ Tpl-> rewrite_rule = array ("/index \. php/"), array ("index.html"); // rewrite rule
$ Tpl-> assign ("indexurl", "index. php ");
$ Tpl-> assign ("str", "I'm a string ");
$ Tpl-> assign ("ec", "I was echo ");
$ Tpl-> assign ("subhtml", "{subtpl tttworkflow this is used to import a template file, which is imported to ttt.htm ");
$ Tpl-> assign ("a", array ('dasdasd'. 'bbbbbbbb', 'cccccccccccccccccccccccc '));
$ Tpl-> assign ("I", 1 );
$ Tpl-> display ("index ");
*/
If (! Defined ("CHARSET") define ("CHARSET", "gb2312"); // character encoding
If (! Defined ("DIR_TPL") define ("DIR_TPL", "tpl"); // default template directory
If (! Defined ("DIR_DATA") define ("DIR_DATA", "data"); // default data directory
If (! Defined ("DEBUG") define ("DEBUG", 0); // default Running Mode
Class template {
// Note var
Public $ rewrite = false; // whether to enable pseudo-static rewrite
Public $ rewrite_rule = array (); // set pseudo-static rules
Public $ defaulttpldir; // default Template
Public $ tpldir; // template directory
Public $ objdir; // compile the cache directory
Public $ tplfile; // Template File
Public $ objfile; // compile the file
Public $ tplid = 1; // template ID
Public $ currdir = 'default'; // directory of the current style
Public $ vars = array (); // note variable table
Public $ removeblanks = false; // remove Spaces
Public $ stdout = 'display'; // output type
Function _ construct ($ tplid, $ currdir ){
$ This-> template ($ tplid, $ currdir );
}
Function template ($ tplid, $ currdir ){
Ob_start ();
If (file_exists (DIR_TPL. '/'. $ currdir )){
$ This-> currdir = $ currdir;
$ This-> tplid = $ tplid;
} Else {
$ This-> currdir = 'default ';
$ This-> tplid = 1;
}
$ This-> defaulttpldir = DIR_TPL. '/default ';
$ This-> tpldir = DIR_TPL. '/'. $ this-> currdir;
$ This-> objdir = DIR_DATA. '/cache/tpl ';
If (version_compare (PHP_VERSION, '5') =-1 ){
Register_shutdown_function (array (& $ this, '_ destruct '));
}
}
// Note publlic
Function assign ($ k, $ v ){
$ This-> vars [$ k] = $ v;
}
// Note publlic
Function display ($ file ){
Extract ($ this-> vars, EXTR_SKIP );
Include $ this-> getObj ($ file );
}
Function getObj ($ file, $ tpldir = ''){
$ Subdir = ($ pos = strpos ($ file, '/') === false? '': Substr ($ file, 0, $ pos );
$ File = $ subdir? Substr ($ file, $ pos + 1): $ file;
$ This-> tplfile = ($ tpldir? $ Tpldir: $ this-> tpldir). '/'. ($ subdir? $ Subdir. '/': ''{.}file.'.htm ';
$ This-> objfile = $ this-> objdir. '/'. ($ tpldir? '': $ This-> tplid. '_'). ($ subdir? $ Subdir. '_': ''). $ file. '. php ';
// Note default directory
If (@ filemtime ($ this-> tplfile) === FALSE ){
$ This-> tplfile = $ this-> defaulttpldir. '/'. ($ subdir? $ Subdir. '/': ''{.}file.'.htm ';
}
// Note to determine if it is relatively expired
If (! File_exists ($ this-> objfile) | DEBUG & @ filemtime ($ this-> objfile) <filemtime ($ this-> tplfile )){
$ This-> compile ();
}
Return $ this-> objfile;
}
Function getTpl ($ file ){
$ Subdir = ($ pos = strpos ($ file, '/') === false? '': Substr ($ file, 0, $ pos );
$ File = $ subdir? Substr ($ file, $ pos + 1): $ file;
$ Tplfile = $ this-> tpldir. '/'. ($ subdir? $ Subdir. '/': ''{.}file.'.htm ';
If (@ filemtime ($ tplfile) ===false ){
$ Tplfile = $ this-> defaulttpldir. '/'. ($ subdir? $ Subdir. '/': ''{.}file.'.htm ';
}
Return $ tplfile;
}
Function compile (){
$ Var_regexp = "\@? \\\$ [A-zA-Z _] \ w *(? : \ [[\ W \. \ "\ '\ [\] \ $] + \]) *";
$ Vtag_regexp = "\ <\? = (\@? \\\$ [A-zA-Z _] \ w *(? : \ [[\ W \. \ "\ '\ [\] \ $] + \]) *) \? \> ";
$ Const_regexp = "\ {([\ w] + )\}";
$ Template = file_get_contents ($ this-> tplfile );
For ($ I = 1; $ I <= 3; $ I ++ ){
If (strpos ($ template, '{subtpl ')! = FALSE ){
If (DEBUG = 2 ){
$ Template = str_replace ('{subtpl', '{tpl', $ template );
} Else {
$ Template = preg_replace ("/[\ n \ r \ t] * \ {subtpl \ s + ([a-z0-9 _: \/] +) \} [\ n \ r \ t] */ies "," file_get_contents (\ $ this-> getTpl ('\ 1') ", $ template );
}
}
}
$ Remove = array (
'/(^ | \ R | \ n) \/\ *. +? (\ R | \ n) \ * \/(\ r | \ n)/is ',
'/\/Note. +? (\ R | \ n)/I ',
'/\/Debug. +? (\ R | \ n)/I ',
'/(^ | \ R | \ n) (\ s | \ t) + /',
'/(\ R | \ n )/',
);
$ This-> removeblanks & $ template = preg_replace ($ remove, '', $ template );
$ Template = preg_replace ("/\ <\! \-\ {(. + ?) \}\-\>/S "," {\\ 1} ", $ template );
$ Template = preg_replace ("/\ {($ var_regexp) \}/", "<? =\\ 1?> ", $ Template );
$ Template = preg_replace ("/\ {($ const_regexp) \}/", "<? =\\ 1?> ", $ Template );
$ Template = preg_replace ("/(? <! \ <\? \=|\\\) $ Var_regexp/"," <? =\\ 0?> ", $ Template );
$ Template = preg_replace ("/\ <\? = (\@? \\\$ [A-zA-Z _] \ w *) (\ [[\ $ \ [\] \ w] + \]) + )\? \>/Ies "," \ $ this-> arrayindex ('\ 1',' \ 2') ", $ template );
$ Template = preg_replace ("/\{\{ eval (.*?) \}\}/Ies "," \ $ this-> str1_tag ('<? \ 1?> ') ", $ Template );
$ Template = preg_replace ("/\ {eval (.*?) \}/Ies "," \ $ this-> strinto tag ('<? \ 1?> ') ", $ Template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {echo \ s + (. + ?) \} [\ N \ r \ t] */ies "," \ $ this-> stripvtag ('<? Echo \ 1;?> ', '')", $ Template );
$ Template = preg_replace ("/\ {(.*?) \}/Ies "," \ $ this-> strinto tag ('<? For (\ 1) {?> ') ", $ Template );
$ Template = preg_replace ("/\ {elseif \ s + (. + ?) \}/Ies "," \ $ this-> strinto tag ('<? } Elseif (\ 1) {?> ') ", $ Template );
For ($ I = 0; $ I <2; $ I ++ ){
$ Template = preg_replace ("/\ {loop \ s + $ vtag_regexp \} (. + ?) \ {\/Loop \}/ies "," \ $ this-> loopsection ('\ 1',' \ 2', '\ 3 ', '\ 4') ", $ template );
$ Template = preg_replace ("/\ {loop \ s + $ vtag_regexp \} (. + ?) \ {\/Loop \}/ies "," \ $ this-> loopsection ('\ 1', '',' \ 2', '\ 3 ') ", $ template );
}
$ Template = preg_replace ("/\ {if \ s + (. + ?) \}/Ies "," \ $ this-> strinto tag ('<? If (\ 1) {?> ') ", $ Template );
$ Template = preg_replace ("/\ {tpl \ s + (\ w + ?) \}/Is "," <? Include \ $ this-> getObj (\ "\ 1 \");?> ", $ Template );
$ Template = preg_replace ("/\ {tpl \ s + (. + ?) \}/Ise "," \ $ this-> stripvtag ('<? Include \ $ this-> getObj (\ "\ 1 \");?> ') ", $ Template );
$ Template = preg_replace ("/\ {tmptpl \ s + (\ w + ?) \}/Is "," <? Include \ $ this-> getObj (\ "\ 1 \", \ $ this-> objdir);?> ", $ Template );
$ Template = preg_replace ("/\ {tmptpl \ s + (. + ?) \}/Ise "," \ $ this-> stripvtag ('<? Include \ $ this-> getObj (\ "\ 1 \", \ $ this-> objdir);?> ') ", $ Template );
$ Template = preg_replace ("/\ {else \}/is", "<? } Else {?> ", $ Template );
$ Template = preg_replace ("/\{\/ if \}/is", "<? }?> ", $ Template );
$ Template = preg_replace ("/\ {\/for \}/is", "<? }?> ", $ Template );
$ Template = preg_replace ("/$ const_regexp/", "<? =\\ 1?> ", $ Template); // note {else} also conforms to the constant format. Pay attention to the order
$ Template = preg_replace ("/(\\\$ [a-zA-Z _] \ w + \ [) ([a-zA-Z _] \ w +) \]/I "," \ 1' \ 2'] ", $ template );
$ Fp = fopen ($ this-> objfile, 'w ');
Fwrite ($ fp, $ template );
Fclose ($ fp );
}
Function arrayindex ($ name, $ items ){
$ Items = preg_replace ("/\ [([a-zA-Z _] \ w *) \]/is", "['\ 1']", $ items );
Return "<? = $ Name $ items?> ";
}
Function stripvtag ($ s ){
$ Vtag_regexp = "\ <\? = (\@? \\\$ [A-zA-Z _] \ w *(? : \ [[\ W \. \ "\ '\ [\] \ $] + \]) *) \? \> ";
Return preg_replace ("/$ vtag_regexp/is", "\ 1", str_replace ("\" ", '"', $ s ));
}
Function loopsection ($ arr, $ k, $ v, $ statement ){
$ Arr = $ this-> stripvtag ($ arr );
$ K = $ this-> stripvtag ($ k );
$ V = $ this-> stripvtag ($ v );
$ Statement = str_replace ("\\\" ", '"', $ statement );
Return $ k? "<? Foreach (array) $ arr as $ k => $ v) {?> $ Statement <?}?> ":" <? Foreach (array) $ arr as $ v) {?> $ Statement <? }?> ";
}
Function _ destruct (){
$ Content = ob_get_contents ();
// Process rewrite
If ($ this-> rewrite ){
$ Arr = $ this-> rewrite_rule;
$ S = $ arr [0];
$ E = $ arr [1];
$ Content = preg_replace ($ s, $ e, $ content );
}
Ob_end_clean ();
Echo $ content;
}
}
$ Tpl = new template ('skin', "default ");
$ Tpl-> objdir = 'tpp ';
$ Tpl-> rewrite = true; // The server must support enabling rewrite.
$ Tpl-> rewrite_rule = array ("/index \. php/"), array ("index.html"); // rewrite rule
$ Tpl-> assign ("indexurl", "index. php ");
$ Tpl-> assign ("str", "I'm a string ");
$ Tpl-> assign ("ec", "I was echo ");
$ Tpl-> assign ("subhtml", "{subtpl tttworkflow this is used to import a template file, which is imported to ttt.htm ");
$ Tpl-> assign ("a", array ('dasdasd'. 'bbbbbbbb', 'cccccccccccccccccccccccc '));
$ Tpl-> assign ("I", 1 );
$ Tpl-> display ("index ");
?>
Create tpl/default/index.html
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
</Head>
<Body>
1. String assignment: <br/>
{$ Str}
<Br/>
2. array assignment: <br/>
{Loop $ a $ v} {$ v}, {/loop}
Or <br/>
{Loop $ a $ key $ val }{$ val}, {/loop}
3. {$ subhtml} <br/>
{Subtpl ttt} <br/>
4. It turns out that I am {$ indexurl} and now I am changed to index. php <br/>
5. I can also echo it. <br/>
{Echo $ ec} <br/>
6. In fact, I can add, subtract, multiply, and divide 6x7x8.
{Echo 6*7*8 ;}
7. What else do you know about the common ones? <br/>
</Body>
</Html>
Create tpl/default/ttt.html
Creating the tpp directory is OK.