コンパイル済み PHP テンプレート エンジンの大まかな実装プロセス_PHP チュートリアル
JTemplate.class.php
001
002
/**
003
* @author ジアウェイ
004
* @2012-6-29 0:23に完了
005
*/
006
クラス JTemplate{
007
//assign 関数で渡された変数には配列が一時的に格納されます
008
プライベート $templateVar = array();
009
//テンプレートディレクトリ
010
プライベート $templateDir = '';
011
//コンパイルディレクトリ
012
プライベート $templateCompileDir = '';
013
014
プライベート $fileName = '';
015
/**
016
* コンストラクター
017
* @param string $templateDir テンプレート ディレクトリ
018
* @param string $templateComplieDir テンプレートのコンパイル ディレクトリ
019
*/
020
パブリック関数 __construct($templateDir,$templateComplieDir){
021
$this->templateDir = $templateDir;
022
$this->templateCompileDir = $templateComplieDir;
023
}
024
/**
025
*テンプレートを表示
026
* @param string $fileName テンプレートファイル名
027
*/
028
パブリック関数表示($fileName){
029
$this->fileName = $fileName;
030
If(file_exists($this->templateDir.'/'.$this->fileName)){
031
$compileFileName = $this->templateCompileDir.'/'.$this->file_safe_name().'.php';
032
If(!file_exists($compileFileName) || filemtime($compileFileName)< filemtime($this->templateDir.'/'.$this->fileName)){
033
$this->del_old_file();
034
$this->compile();
035
}
036
extract($this->templateVar);
037
include $compileFileName;
038
}その他{
039
$this->error('申し訳ありませんが、テンプレート ファイル '.$this->fileName.' は存在しません!!');
040
}
041
}
042
/**
043
* コンパイルされたファイル名を取得します
044
*/
045
プライベート関数 get_compile_file(){
046
$compileFile =explode('.',$this->fileName);
047
unset($compileFile[count($compileFile)-1]);
048
return implode('.',$compileFile);
049
}
050
/**
051
* コンパイル
052
*/
053
プライベート関数compile(){
054
$fileHandle = @fopen($this->templateDir.'/'.$this->fileName, 'r');
055
while(!feof($fileHandle)){
056
$fileContent = fread($fileHandle,1024);
057
}
058
fclose($fileHandle);
059
$fileContent = $this->template_replace($fileContent);
060
//$compileFile = $this->get_compile_file($fileName);
061
$fileHandle = @fopen($this->templateCompileDir.'/'.$this->file_safe_name().'.php','w');
062
if($fileHandle){
063
fwrite($fileHandle, $fileContent);
064
fclose($fileHandle);
065
}その他{
066
$this->error('申し訳ありません、コンパイル ディレクトリに書き込めません!');
067
}
068
}
069
/**
070
* 値を渡すテンプレート
071
* @param string $valueName テンプレートで使用される変数名
072
* @param $value 変数値
073
*/
074
パブリック関数 assign($valueName,$value){
075
$this->templateVar[$valueName] = $value;
076
}
077
078
/**
079
*テンプレート定期交換
080
* @param string $content 置換コンテンツ
081
* @return 置換されたコンテンツ
082
*/
083
プライベート関数 template_replace($content){
084
$orginArray = 配列(
085
'//s',
086
'//s',
087
'/(.+?)/s',
088
'//s',
089
'//s',
090
'//s',
091
'//s',
092
'//s',
093
'/{P:(.+?):}/s',
094
'/{C:(w+)}/s',
095
'/{I:(.+?)}/s',
096
'/{F:(.+?)}/s',
097
'/{EF:(.+?)}/s',
098
'/{([a-zA-Z0-9_[]'"$.x7f-xff]+)}/s',
099
);
100
101
$changeArray = 配列(
102
'',
103
'$$3){$countLoop++;?>',
104
'$1',
105
'',
106
'',
107
'',
108
'',
109
'',
110
'',
111
'',
112
'templateDir.'/$1";?>',
113
'',
114
'',
115
'',
116
);
117
return $repContent=preg_replace($orginArray,$changeArray,$content);
118
}
119
/**
120
* 删除旧文件
121
*/
122
private function del_old_file(){
123
$compileFile = $this->get_compile_file($this->fileName);
124
$files = glob($this->templateCompileDir.'/'.$compileFile.'*.php');
125
// print_r($files);
126
if($files){
127
@unlink($files[0]);
128
}
129
}
130
/**
131
* 编译文件名安全处理方法
132
* @return string 返回编译文件名
133
*/
134
private function file_safe_name(){
135
$compileFile = $this->get_compile_file($this->fileName);
136
return $compileFile.filemtime($this->templateDir.'/'.$this->fileName);
137
}
138
139
/**
140
* 错误输出函数
141
* @param string $content 错误输出信息
142
*/
143
private function error($content){
144
$stringHtml = '
145
$stringHtml .= 'Error information:
';
146
$stringHtml .= '';
147
$stringHtml .= $content;
148
$stringHtml .= '';
149
$stringHtml .= '
150
exit($stringHtml);
151
}
152
}
153
?>
インデックス.php
ソースプリントを表示しますか?
01
02
/**
03
* @著者 Jiawei
04
*/
05
include_once 'JTemplate/JTemplate.class.php';
06
$template = new JTemplate('template','compile');
07
$a = array('a','b','c','d');
08
定義('_CORE_','aaa');
09
$template->assign('a', $a);
10
$template->display('index.html');
11
?>
著者:袁家伟

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

CakePHP へのログインは非常に簡単な作業です。使用する関数は 1 つだけです。 cronjob などのバックグラウンド プロセスのエラー、例外、ユーザー アクティビティ、ユーザーが実行したアクションをログに記録できます。 CakePHP でのデータのログ記録は簡単です。 log()関数が提供されています

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、
