Smarty最大的功能是做模版的頁面緩衝。也就是通過Smarty可以完成兩個步驟:編譯+解析
第一步:編譯。是指把模版檔案的標籤替換為純php,再儲存在緩衝位置,儲存的副檔名是PHP,我把這個步驟叫做編譯(這是我自己的叫法,不是官方的)
第二步:解析。也就是把剛才編譯的PHP檔案解析執行而已~~這個就不用多做解釋了
切入正題,在Smarty.class.php檔案中加入如下代碼
function MakeHtmlFile($file_name, $content)
{ //目錄不存在就建立
if (!file_exists (dirname($file_name))) {
if (!@mkdir (dirname($file_name), 0777)) {
die($file_name."目錄建立失敗!");
}
}
if(!$fp = fopen($file_name, "w")){
echo "檔案開啟失敗!";
return false;
}
if(!fwrite($fp, $content)){
echo "檔案寫入失敗!";
fclose($fp);
return false;
}
fclose($fp);
chmod($file_name,0666);
}
這個函數的作用就是儲存檔案~~
調用方法如下
require '../libs/Smarty.class.php';
$smarty = new Smarty;
//…………省略變數定義和賦值
//$smarty->display('index.tpl');
$content=$smarty->fetch("index.tpl");
$smarty->MakeHtmlFile('./index.html',$content);//產生
摘自 PainsOnline的專欄
http://www.bkjia.com/PHPjc/478329.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478329.htmlTechArticleSmarty最大的功能是做模版的頁面緩衝。也就是通過Smarty可以完成兩個步驟:編譯+解析 第一步:編譯。是指把模版檔案的標籤替換為純php,再...