Kangsheng 製品から抽出したテンプレート クラス (discuz)_PHP チュートリアル

WBOY
リリース: 2016-07-21 15:27:27
オリジナル
871 人が閲覧しました

コードをコピーします コードは次のとおりです:

/*template.class.php
@ Kangsheng Weibo テンプレート抽出クラス このテンプレートは時間をかけて独自に作成すると使いやすいと思います。by Lei Rijin
@ ctt テンプレートを見てみました。phpcms テンプレートに似ていますか? ? ^_^ ふふふ! ! !
@ Weibo http://weibo.com/lrjxgl
@ 誰もが良いものを共有しています。抽出について質問がある場合は、お問い合わせください
@ デフォルトのテンプレート ファイルは .htm です
$tpl = new template('skin' ,"default" );
$tpl->objdir='tpp';
$tpl->rewrite=true;//書き換えを有効にするにはサーバーのサポートが必要です
$tpl->rewrite_rule=array("/index .php/" ),array("index.html")); //ルールを書き換えます
$tpl->assign("indexurl","index.php");
$tpl->assign("str" ,"私は String la la la");
$tpl->assign("ec","エコーアウトされました");
$tpl->assign("subhtml","{subtpl ttt})テンプレートファイルを導入するには、ttt.htm");
$tpl->assign("a",array('dasdasd'.'bbbbbbb','cccccccccccccc'));
$tpl-> ; assign("i",1);
$tpl->display("index");
if(!define("CHARSET"));//文字エンコーディング
if(!define("DIR_TPL"))define("DIR_TPL","tpl");//デフォルトのテンプレートディレクトリ
if(!define("DIR_DATA"))define("DIR_DATA","data") ; //デフォルトのデータディレクトリ
if(!define("DEBUG"))define("DEBUG",0);//デフォルトの実行モード
class template {
//note var
public $rewrite=false;//有効になっています 擬似静的書き換え
public $rewrite_rule=array(); //擬似静的ルールを設定します
public $defaulttpldir;//デフォルトのテンプレート
public $tpldir;//テンプレート ディレクトリ
public $objdir;//コンパイル キャッシュディレクトリ
public $ tplfile;//テンプレートファイル
public $objfile;//コンパイルされたファイル
public $tplid=1;//テンプレート番号
public $currdir='default';//現在のスタイルディレクトリ
public $vars=array ();/ /メモ変数テーブル
public $removeblanks=false;//スペースを削除
public $stdout='display';//出力タイプ
function __construct($tplid, $currdir) {
$this->template ($tplid , $currdir);
} 関数テンプレート($tplid, $currdir) {
ob_start();
if(file_exists(DIR_TPL.'/'.$currdir)) {
$this->currdir = $currdir;
$this->tplid = $tplid; else {
$this->tplid = 1; DIR_TPL.' /default';
$this->tpldir = DIR_TPL.'/'.$this->objdir = DIR_DATA.'/cache/tpl'; PHP_VERSION, ' 5') == -1) {
register_shutdown_function(array(&$this, '__destruct'))
}
}
//note public
function assign($k, $v) {
$this -> vars[$k] = $v;
//note public
function display($file) {
include $this->getObj($)ファイル);
}
関数 getObj($file, $tpldir = '') {
$subdir = ($pos = strpos($file, '/')) === false '' : substr($file, 0, $ pos);
$file = $subdir ? substr($file, $pos + 1) : $file = ($tpldir ? $tpldir : $this->tpldir) '/' .($subdir ? $subdir.'/' : '').$file.'.htm';
$this->objfile = $this->objdir.'/'.($tpldir ? '' : $this->tplid.'_').($subdir ? $subdir.'_' : '').$file.'.php';
//デフォルトのディレクトリに注意してください
if(@filemtime( $this->tplfile) === FALSE) {
$this->tplfile = $this->defaulttpldir.'/'.($subdir ? $subdir.'/' : '').$file. '.htm ';
}
//note 比較が期限切れかどうかを判断します
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 ? : $file = $this->tpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
if(@filemtime($tplfile) === FALSE) {
$tplfile = $this ->defaulttpldir.'/'.($subdir ? $subdir.'/' : '').$file.'.htm';
}
return $tplfile
}
functioncompile( ) {
$var_regexp = "@?\$[a-zA-Z_]w*(?:[[w."'[]$]+])*";
$vtag_regexp = ""
$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);
}
}
}
$削除 = 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("/