Copy CodeThe code is as follows:
/*template.class.php
@ Kang Shengweibo Template Extraction class think this template useful to spend some time independent out. by Rezhigin
@ have a look at CTT this template is similar to Phpcms's template?? ^_^ Hey!!!
@ Micro Bo Http://weibo.com/lrjxgl
@ Good things everyone sharing stumbling pull out there's a problem please come up
The @ template file defaults to. htm
$TPL = new Template (' Skin ', ' default ');
$tpl->objdir= ' TPP ';
Server support required to open rewrite $tpl->rewrite=true;//
$TPL->rewrite_rule=array (Array ("/index\.php/"), Array ("index.html")); Rewrite rules
$tpl->assign ("Indexurl", "index.php");
$TPL->assign ("str", "I'm the string cheerleader");
$TPL->assign ("EC", "I was echo out");
$tpl->assign ("subhtml", "{SUBTPL TTT} This is used to introduce a template file, this is the introduction of ttt.htm");
$tpl->assign ("A", Array (' DASDASD '). Bbbbbbb ', ' CCCCCCCCCCCCCC '));
$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 directory
if (!defined ("Debug")) Define ("Debug", 0);//default run mode
Class Template {
Note Var
Public $rewrite =false;//whether to turn on pseudo-static rewrite
Public $rewrite _rule=array (); Set pseudo-static rules
public $defaulttpldir;//Default Template
public $tpldir;//Template directory
public $objdir;//Compile Cache directory
public $tplfile;//template file
public $objfile;//Compile file
Public $tplid =1;//Template number
Public $currdir = ' default ';//Current style directory
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 the comparison expires
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->stripvtag (' '), $template);
$template = Preg_replace ("/\{eval (. *?) \}/ies "," \ $this->stripvtag (' '), $template);
$template = Preg_replace ("/[\n\r\t]*\{echo\s+ (. +?) \}[\n\r\t]*/ies "," \ $this->stripvtag (' ', '), $template);
$template = Preg_replace ("/\{for (. *?) \}/ies "," \ $this->stripvtag (' '), $template);
$template = Preg_replace ("/\{elseif\s+ (. +?) \}/ies "," \ $this->stripvtag (' '), $template);
for ($i =0; $i <2; $i + +) {
$template = Preg_replace ("/\{loop\s+ $vtag _regexp\s+ $vtag _regexp\s+ $vtag _regexp\} (. +?) \{\/loop\}/ies "," \ $this->loopsection (' \\1 ', ' \\2 ', ' \\3 ', ' \\4 ') ", $template);
$template = Preg_replace ("/\{loop\s+ $vtag _regexp\s+ $vtag _regexp\} (. +?) \{\/loop\}/ies "," \ $this->loopsection (' \\1 ', ' ', ' \\2 ', ' \\3 ') ", $template);
}
$template = Preg_replace ("/\{if\s+ (. +?) \}/ies "," \ $this->stripvtag (' '), $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, where it is important to note the sequencing
$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 ();
Handling rewrite
if ($this->rewrite) {
$arr = $this->rewrite_rule;
$s = $arr [0];
$e = $arr [1];
$c
}
Ob_end_clean ();
Echo $content;
}
}
$TPL = new Template (' Skin ', ' default ');
$tpl->objdir= ' TPP ';
Server support required to open rewrite $tpl->rewrite=true;//
$TPL->rewrite_rule=array (Array ("/index\.php/"), Array ("index.html")); Rewrite rules
$tpl->assign ("Indexurl", "index.php");
$TPL->assign ("str", "I'm the string cheerleader");
$TPL->assign ("EC", "I was echo out");
$tpl->assign ("subhtml", "{SUBTPL TTT} This is used to introduce a template file, this is the introduction of ttt.htm");
$tpl->assign ("A", Array (' DASDASD '). Bbbbbbb ', ' CCCCCCCCCCCCCC '));
$tpl->assign ("I", 1);
$TPL->display ("index");
?>
New tpl/default/index.html
Copy the Code code as follows:
Untitled Document
1. Assigning values to Strings:
{$STR}
2. Assigning values to arrays:
{Loop $a $v} {$v},{/loop}
Or
{Loop $a $key $val} {$val},{/loop}
3.{$subhtml}
{SUBTPL TTT}
4. I was {$indexurl} and now I'm index.php.
5. I can still echo it.
{echo $ec}
6. In fact, I can also subtraction 6*7*8
{echo 6*7*8;}
7. What you don't understand is what you're used to.
New tpl/default/ttt.html
New TPP directory OK.
The above describes the Discuz x2.5 from the Kang Sheng products discuz extracted from the template class, including the Discuz x2.5 aspects of the content, I hope that the PHP tutorial interested friends have helped.