乙個自己寫的PHP模板引擎

2022-03-16 05:21:09 字數 2480 閱讀 6151

這是乙個自己寫的編譯型的模板引擎(不包括快取部分)貼上**。有問題可以隨時跟帖。

單檔案版:jtemplate.class.php

templatedir = $templatedir;

$this->templatecompiledir = $templatecompliedir;

}/**

* 顯示模板方法

* @param string $filename 模板檔名

*/public function display($filename)

extract($this->templatevar);

include $compilefilename;

}else

}/**

* 獲取編譯檔名方法

* @param string $filename 模板檔名

*/public function get_compile_file($filename)

/*** 編譯方法

* @param string $filename 模板檔名

*/public function compile($filename)

fclose($filehandle);

$filecontent = $this->template_replace($filecontent);

//$compilefile = $this->get_compile_file($filename);

$filehandle = @fopen($this->templatecompiledir.'/'.$this->file_safe_name($filename).'.php','w');

if($filehandle)else

}/**

* 模板傳值方法

* @param string $valuename 模板中使用的變數名

* @param all $value 變數值

*/public function assign($valuename,$value)

/*** 模板正則替換方法

* @param string $content 替換內容

* @return string 替換過後的內容

*/public function template_replace($content)/s',

'//s',

'/(.+?)/s',

'//s',

'//s',

'//s',

'//s',

'//s',

'/\/s',

'/\/s',

'/\/s',

'/\/s',

'/\/s',

'/\/s',

);$changearray = array(

'','$$3)

/*** 刪除舊檔案

* @param string $filename 模板檔名

*/public function del_old_file($filename)

}/**

* 編譯檔名安全處理方法

* @param string $filename 傳入模板檔名

* @param string 返回編譯檔名

*/public function file_safe_name($filename)

/*** 錯誤輸出函式

* @param string $content 錯誤輸出資訊

*/public function error($content) } ?>

模板引擎使用方法:

首先載入模板引擎核心檔案jtemplate.class.php

include_once 'jtemplate/jtemplate.class.php';

例項化模板引擎:

$template = new jtemplate(模板目錄,編譯目錄);

模板引擎方法:

1.assign方法用來將值傳入模板中

$template->assign('模板引擎中用的變數名');

2.display顯示模板檔案方法:

$template->display('模板檔案');

模板語法使用方法:

輸出變數

//不帶$符號

判斷語句

內容1內容2內容3//可拆分使用

遍歷陣列

迴圈內容

相當於foreach($a as $v){}

遍歷陣列2:

迴圈內容當陣列為空或傳入變數不為陣列的時候輸出的內容

遍歷中的計數器

要在遍歷中使用計數器可以使用變數來計算當前迴圈的次數如果在if中使用請使用$countloop

//在迴圈內輸出當前迴圈的次數。在迴圈外使用輸出最近的一次迴圈迴圈了多少次

//if語句中這樣使用

輸出常量

載入檔案

執行函式不輸出

執行函式並輸出函式返回的結果

執行php原生語句

寫乙個自己的遊戲引擎

以下是我的一點看法,另外推薦一些學習的資料 ps 小弟才搞3d一年的時間,還是cn,高手不要笑話,還請大家多多指點 首先是語言方面,這個問題沒有太多的選擇的餘地,基本上只有用c 現在還在堅持用純c來寫引擎的估計只有卡馬克同學了 語言的功底是特別重要的了 大規模專案的編寫能力也是很重要的,推薦看 大規...

毛毛蟲教你寫乙個屬於自己的模板引擎

phpchina首發 smarty一直被人視為是多餘的東西,我覺得認為smarty多餘的人才是多餘的.不說這些了。今天我就教大家寫個模板引擎,讓大家都可以寫乙個屬於自己的模板引擎,而且看完這篇文章之後,你對smarty的認識會更進一步的。我的模板引擎名叫stupid 傻瓜 的意思 我不喜歡太聰明的東...

自己寫的乙個PHP上傳類

主要功能 檔案上傳,獲取檔名,獲取檔案大小,隨機生成新檔名,獲取檔案型別,生成縮圖,返回縮略 件名,返回上傳後生成的檔案的檔名,返回上傳後的檔案路徑 class ieb upload if dirpath else 檢查檔案是否存在 function scanfile return scan 獲取檔...