Template class extracted from the discuz product

Source: Internet
Author: User
Templates extracted from the discuz product. For more information about learning php, see. The code is as follows:
/* 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 )\}/"," ", $ Template );
$ Template = preg_replace ("/\ {($ const_regexp )\}/"," ", $ Template );
$ Template = preg_replace ("/(? ", $ Template );
$ Template = preg_replace ("/\ <\? = (\@? \\\$ [A-zA-Z _] \ w *) (\ [[\ $ \ [\] \ w] + \]) + )\? \>/Ies "," \ $ this-> arrayindex ('\ 1',' \ 2') ", $ template );
$ Template = preg_replace ("/\{\{ eval (.*?) \}\}/Ies "," \ $ this-> str1_tag (' ') ", $ Template );
$ Template = preg_replace ("/\ {eval (.*?) \}/Ies "," \ $ this-> str1_tag (' ') ", $ Template );
$ Template = preg_replace ("/[\ n \ r \ t] * \ {echo \ s + (. + ?) \} [\ N \ r \ t] */ies "," \ $ this-> stripvtag (' ', '')", $ Template );
$ Template = preg_replace ("/\ {(.*?) \}/Ies "," \ $ this-> str1_tag (' ') ", $ Template );
$ Template = preg_replace ("/\ {elseif \ s + (. + ?) \}/Ies "," \ $ this-> str1_tag (' ') ", $ 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-> str1_tag (' ') ", $ Template );
$ Template = preg_replace ("/\ {tpl \ s + (\ w + ?) \}/Is "," GetObj (\ "\ 1 \");?> ", $ Template );
$ Template = preg_replace ("/\ {tpl \ s + (. + ?) \}/Ise "," \ $ this-> stripvtag (' GetObj (\ "\ 1 \");?> ') ", $ Template );
$ Template = preg_replace ("/\ {tmptpl \ s + (\ w + ?) \}/Is "," GetObj (\ "\ 1 \", \ $ this-> objdir);?> ", $ Template );
$ Template = preg_replace ("/\ {tmptpl \ s + (. + ?) \}/Ise "," \ $ this-> stripvtag (' GetObj (\ "\ 1 \", \ $ this-> objdir);?> ') ", $ Template );
$ Template = preg_replace ("/\ {else \}/is "," ", $ Template );
$ Template = preg_replace ("// \{\/ if \}/is "," ", $ Template );
$ Template = preg_replace ("// \{\/ for \}/is "," ", $ Template );
$ Template = preg_replace ("/$ const_regexp /"," ", $ 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" ";
}
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? " $ V) {?> $ Statement ":" $ 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
The code is as follows:




Untitled Document



1. string assignment:

{$ Str}


2. array assignment:

{Loop $ a $ v} {$ v}, {/loop}
Or

{Loop $ a $ key $ val }{$ val}, {/loop}

3. {$ subhtml}

{Subtpl ttt}

4. I used to be {$ indexurl} and now I am changed to index. php.

5. I can also echo it.

{Echo $ ec}

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?





Create tpl/default/ttt.html
Creating the tpp directory is OK.

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.