シンプルなテンプレートクラス(PHP)_PHPチュートリアル

WBOY
リリース: 2016-07-13 17:51:48
オリジナル
869 人が閲覧しました

データ操作クラスでは、プロジェクトは単純にデータを操作することしかできませんが、アーティストと一緒に美しいページを表示できるようにするには、より優れたテンプレート エンジンが必要です。 SMARTY のような比較的大きなテンプレート エンジンと比較すると、次のテンプレート エンジンは非常に小さいと思います。

以前ネットでこのテンプレートクラスを拝見し、とても良く書かれていたので引用しましたが、作者がまだ分からないので、先にこのクラスの原理についてお話します。

まず第一に、このクラスには単純な通常のパーサーしかありません。でも基本的には使えます。これをベースに拡張することができれば、この小さなことは大きな発展をもたらすと私は信じています。同じ趣味を持つ同志がそれを強化するために参加するよう招待されます。今からレンガを落としていきます。

template.class.php

[html]
クラステンプレート{

//変数に格納される配列
プライベート $vars = array(); //テンプレートディレクトリ
パブリック $template_dir = './template/'; //キャッシュディレクトリ
パブリック $cache_dir = './cache/'; //コンパイルディレクトリ
パブリック $template_c_dir = './template_c/'; //テンプレートファイル
パブリック $template_file = ''; //左コネクタ
パブリック $left_delimiter = '<{'; //右コネクタ
パブリック $right_delimiter = '}>'; // ファイルをコンパイルします
プライベート $template_c_file = ''; //ファイルをキャッシュします
プライベート $cache_file = ''; //キャッシュ時間
パブリック $cache_time = 0;
//組み込みパーサー
プライベート $preg_temp = array(
'~<{($[a-z0-9_]+)}>~i'
=> '
'~<{($[a-z0-9_]+).([a-z0-9_]+)}>~i'
=> '', // <{$arr.key}>
'~<{($[a-z0-9_]+).([a-z0-9_]+).([a-z0-9_]+)}>~i'
=> '', // <{$arr.key.key2}>
'~~i'
=> '_include($2); ?>', //
'~<{:(.+?)}>~' => '', // <{:strip_tags($a)>

'~<{~(.+?)}>~' => '', // <{~var_dump($a)>

'~ ' );

/**
*コンストラクター
​​*/
パブリック関数 __construct(){
If(定義('TMP_PATH')){
$this->template_c_dir = 'template_c/'; $ this-&gt; cache_dir }
}

/**
*変数の代入
*@param $key 混合キー名
*@param $value 混合値
​​*/
パブリック関数 assign($key, $value = ''){
If(is_array($key)){
$this->vars=array_merge($key,$this->vars); }
その他{
$this->vars[$key]=$value; }
}

/**
*ページを表示
*@param $file 文字列テンプレートファイル名
​​*/
パブリック関数表示($file){
echo $this->fetch($file); }

/**
*キャッシュコンテンツに戻る
*@param $file 文字列テンプレートファイル名
*@return $content 文字列キャッシュコンテンツ
​​*/
パブリック関数 fetch($file){
$this->template_file = $file; $desc_template_file = $this->template_dir .$file; $desc_content = $this->readfile($desc_template_file);
$template_c_file_time= filemtime($desc_template_file); //キャッシュ時間を超えたらコンパイル
If($this->cache_time < time()-$template_c_file_time){
$this->complie($this->token($desc_content)); }
//以下のキャッシュ領域の内容を取得します

ob_start();
@extract($this->vars, EXTR_OVERWRITE); include ($this->template_c_dir . $this->template_c_file);
$content = ob_get_contents(); ob_end_clean();
//$this->store_buff($content);
$content を返します。 }

/*
*区切り文字を置き換えて、パーサーの内容を置き換えます
*@param $content string コンテンツを読み取ります
*@return $token_content 文字列がコンテンツに置き換えられました
*/
パブリック関数トークン($content){
$token_content = $content; If($left_delimiter != '<{'){
$token_content = str_replace($left_delimiter, '<{', $token_content); $token_content = str_replace($right_delimiter, '}>' , $token_content); }
$token_content = preg_replace(array_keys($this->preg_temp), $this->preg_temp, $token_content); $token_content を返す
}

/*
*ストレージを生成します
*@param $content string コンテンツを読み取ります
*

パブリック関数store_buff($content){
$this->cache_file = md5($this->template_file) '.html'; $tempfile = $this->cache_dir . $this->cache_file; $fp = fopen($tempfile, 'w'); fputs($fp,$content); fclose($fp);設定解除($fp)
}
*/

/*
*コンパイルして保存します
*@param $content string コンテンツを読み取ります
*
*/
パブリック関数 complie($content){
$this->template_c_file = md5($this->template_file) '.php'; $tempfile = $this->template_c_dir . $this->template_c_file; $fp = fopen($tempfile, 'w'); fputs($fp,$content); fclose($fp); 設定解除($fp)
}

/*
*ファイルの内容を読んでください
*@param $file string ファイル名
*@return $content 文字列ファイルの内容
*/
パブリック関数 readfile($file){
If(file_exists($file)){
$fp = fopen($file, 'r'); $content =''; ながら(!feof($fp)){
$content .= fgets($fp,4096);                                                                        fclose($fp); unset($fp); $content を返します。 }
その他{
exit($file . ' 存在しません!'); }
}

/*
*テンプレートのネスト
*@param $file string ファイル名
*@return string ファイルの絶対アドレス
*/
パブリック関数 _include($file){
If(file_exists($this->template_dir . $file)){
return ($this->template_dir . $file); }
その他{
echo "テンプレート ファイルが存在しません"; 出る
}
} www.2cto.com
}
?>

このテンプレートを使用すると、アーティストがスライス ファイルに多くの SMARTY マークを書き込む必要がなく、画像や CSS を変更せずに直接使用できます。 jsアドレス。

作者: トミージョン



http://www.bkjia.com/PHPjc/478157.html

www.bkjia.com

tru​​e

http://www.bkjia.com/PHPjc/478157.html

技術記事

データ操作クラスでは、プロジェクトは単純にデータを操作することしかできませんが、アーティストと一緒に美しいページを表示できるようにするには、より優れたテンプレート エンジンが必要です。 SMAとは...

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!