Template classes extracted from Kang Sheng Products (discuz)

Source: Internet
Author: User
Copy CodeThe code is as follows:
<?php
/*template.class.php
@ Kang Shengweibo Template Extraction class feel this template easy to spend some time independent. by Rezhigin
@ Look at CTT This template is similar to Phpcms's template?? ^_^ Hey!!!
@ Micro Bo Http://weibo.com/lrjxgl
@ Good things everyone share the stumbling of the extraction of the problem please put forward
The @ template file defaults to. htm
$TPL = new Template (' Skin ', ' default ');
$tpl->objdir= ' TPP ';
$TPL->rewrite=true;//Open rewrite requires server support
$TPL->rewrite_rule=array (Array ("/index\.php/"), Array ("index.html")); Rewrite rules
$tpl->assign ("Indexurl", "index.php");
$TPL->assign ("str", "I'm a string cheerleader");
$TPL->assign ("EC", "I was echo out");
$tpl->assign ("subhtml", "{SUBTPL TTT}" which 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 data 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;//to 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) \}/", "<?=\\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->stripvtag (';? \\1?> ') ", $template);
$template = Preg_replace ("/\{eval" (. *?) \}/ies "," \ $this->stripvtag (';? \\1?> ') ", $template);
$template = Preg_replace ("/[\n\r\t]*\{echo\s+" (. +?) \}[\n\r\t]*/ies "," \ $this->stripvtag (';? echo \\1;?> ', ') ", $template);
$template = Preg_replace ("/\{for" (. *?) \}/ies "," \ $this->stripvtag (';? for (\\1) {?> ') ", $template);
$template = Preg_replace ("/\{elseif\s+" (. +?) \}/ies "," \ $this->stripvtag (';?} ElseIf (\\1) {?> ') ", $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 (';? 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, where you should note the sequence
$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 ();
Handling 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;//Open rewrite requires server support
$TPL->rewrite_rule=array (Array ("/index\.php/"), Array ("index.html")); Rewrite rules
$tpl->assign ("Indexurl", "index.php");
$TPL->assign ("str", "I'm a string cheerleader");
$TPL->assign ("EC", "I was echo out");
$tpl->assign ("subhtml", "{SUBTPL TTT}" which 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 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 ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>

<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. I am {$indexurl} Now I have been turned into index.php<br/>
5. I can also echo out <br/>
{echo $ec}<br/>
6. In fact, I can also subtraction 6*7*8
{echo 6*7*8;}
7. That's all that's used. What else do not understand <br/>

</body>

New tpl/default/ttt.html
New TPP catalog 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.