如何通過smarty產生靜態化網頁_PHP教程

來源:互聯網
上載者:User
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,再...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.