discuz x2.5 從康盛產品discuz提取出來的模板類

WBOY
發布: 2016-07-29 08:45:49
原創
994 人瀏覽過

複製程式碼 程式碼如下:


/*template.class.php
@康盛微博 模板提取類別 覺得這個模板好用 花點時間獨立出來。by 雷日錦
@看了一下ctt 這個模板 跟 phpcms的模板類似 難道? ? ^_^ 嘿嘿! ! !
@ 微博http://weibo.com/lrjxgl
@ 好東西大家共享磕磕絆絆的提取出來有問題請提出來
@ 模板文件預設為.htm
$tpl = new template ('skin',"default");
$tpl->objdir='tpp';
$tpl->rewrite=true;//開啟rewrite 需要伺服器支援
$tpl->rewrite_rule= array(array("/index.php/"),array("index.html")); //rewrite規則
$tpl->assign("indexurl","index.php");
$tpl->assign("str","我是字串啦啦啦");
$tpl->assign("ec","我是被echo出來的");
$tpl- >assign("subhtml","{subtpl ttt}這是用來引入一個模板檔案的,這就是引入ttt.htm");
$tpl->assign("a",array('dasdasd'. 'bbbbbbb','cccccccccccccc'));
$tpl->assign("i",1);
$tpl->display("index");
*/
if( !defined("CHARSET")) define("CHARSET","gb2312");//字元編碼
if(!defined("DIR_TPL")) define("DIR_TPL","tpl");//預設範本目錄
if(!defined("DIR_DATA")) define("DIR_DATA","data");//預設資料目錄
if(!defined("DEBUG")) define("DEBUG", 0);//預設運作模式
class template {
//note var
public $rewrite=false;//是否開啟偽靜態rewrite
public $rewrite_rule=array(); //設定偽靜態規則
public $defaulttpldir;//預設的範本
public $tpldir;//範本目錄
public $objdir;//編譯快取目錄
public $tplfile;//範本檔案
public $objfile;//編譯檔案
public $tplid=1;//範本編號
public $currdir='default';//目前風格目錄
public $vars=array() ;//note 變數表
public $removeblanks=false;//移除空格
public $stdout='display';//輸出型別
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 預設目錄
if(@filemtime($this->tplfile) === FALSE) {
$this->tplfile = $this->defaulttpldir.'/'.($subdir ? $subdir.'/' : '' ).$file.'.htm';
}
//note 判斷是否比較過期
if(!file_exists($this->objfile) || DEBUG && @filemtime($this->objfile ) 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 if(strpos($template, '{subtpl') !== FALSE) {
if(DEBUG == 2) {
$template = str_replace('{subtpl ', '{tpl ', $template);
} else {
$template = preg_replace("/[nrt]*{subtpls+([a-z0- 9_:/]+)}[nrt]*/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("/(?", $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("/[nrt]*{echos+ (.+?)}[nrt]*/ies", " $this->stripvtag(' echo \1; ?>','')", $template);
$template = preg_replace("/{for (.*?)}/ies", "$ this->stripvtag(' for(\1) {?>')", $template); = preg_replace("/{elseifs+(.+?)}/ies", "$this->stripvtag('')", $template); for($i=0; $i$template = preg_replace("/{loops+$vtag_regexps+$vtag_regexps+ $vtag_regexp}(.+?){/loop}/ies", "$this->loopsection('\1', '\2', '\3', '\4')", $template);
$template = preg_replace("/{loops+$vtag_regexps+$vtag_regexp}( .+?){/loop}/ies", "$this->loopsection('\1', '', '\2', ' \3')", $模板);
}
$template = preg_replace("/{ifs+(.+?)}/ies", "$this->stripvtag(' if(\1) { ?>')" ,$template );
$template = preg_replace("/{tpls+(w+?)}/is", " include $this->getObj("\1");?>", $template); $template = preg_replace("/{tpls+(.+?)}/ise", "$this->stripvtag(' include $this->getObj("\1"); ?> ;')", $模板);
$template = preg_replace("/{tmptpls+(w+?)}/is", " include $this->getObj("\1", $this->objdir);?>" , $模板) ;
$template = preg_replace("/{tmptpls+(.+?)}/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} 也符合常數格式,此處要注意流程圖順序
$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);
返回“=$name$items?>”;
}
function stripvtag($s) {
$vtag_regexp = "=(@?\$[a-zA-Z_]w*(?:[[w."'[] $ ]+])*)?>";
return preg_replace("/$vtag_regexp/is", "\1", str_replace("\"", '"', $s));
}
函數loopsection($arr, $k, $v, $statement) {
$arr = $this->stripvtag($arr);
$k = $this->stripvtag( $k);
$v = $this->stripvtag($v);
$statement = str_replace("\"", '"', $statement);
回傳$k ? )$arr as $k => $v) {?>$statement}?>" : " foreach((array)$arr as $v) {?>$statement } ?>";
}
函數 __destruct() {
$content = ob_get_contents();
//處理重寫
if($this->rewrite) {
$arr=$this->rewrite_rule
$s=$arr[0];
$e=$arr[1];
$c
}
ob_end_clean() ;
回顯$內容;
}
$tpl = 新模板('skin',"default");
$tpl->objdir='tpp'; >$tpl->rewrite=true;//開啟重寫需要伺服器支援
$tpl->rewrite_rule=array(array("/index.php/"),array("index.html")); // 重寫規則
$tpl->assign("indexurl","index.php");
$tpl->assign("str","我是字串啦啦");
$tpl->assign("ec","我是被echo出來的");
$tpl->assign("subhtml","{subtpl ttt} 是用來引入一個模板檔案的,這就是引入 ttt.htm");
$tpl->assign("a",array('dasdasd'.'bbbbbbbbbb','cccccccccccccccc'));
$tpl->;分配("i",1);
$tpl->display("索引");


新 tpl/default/index.html



複製程式碼


程式碼如下:






無標題文件


1.字串賦值:

{$str}


2.陣列賦值:

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


{loop $a $key $val } {$val},{/loop}
3.{$subhtml}

{subtpl ttt}

4.原來我是{$indexurl } 現在我被變成了index.php

5.我還可以echo 出來呢

{echo $ec}

6.其實我還可以加減乘除的6* 7*8
{echo 6*7*8;}
7.常用的就這些了還有什麼不懂的




新建tpl/default/ttt.html
新建tpp目錄ok了

以上就介紹了discuz x2.5 從康盛產品discuz提取出來的模板類,包括了discuz x2.5方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板