smarty產生靜態頁面的方法

來源:互聯網
上載者:User
範例程式碼:require('libs/Smarty.class.php');$tpl=new Smarty();$tpl->template_dir='./templates/';$tpl->compile_dir='./templates_c';$tpl->config_di

範例程式碼:require('libs/Smarty.class.php');$tpl=new Smarty();$tpl->template_dir='./templates/';$tpl->compile_dir='./templates_c';$tpl->config_dir='./config/';$tpl->cache_dir='./cache/';$tpl->left_delimiter='<{';$tpl->right_delimiter='}>';ob_start(); //開啟輸出緩衝區

$tpl->assign('s_title',$_POST['title']);//設定網站標題//以下為接受傳遞過來的變數並賦值到模板頁$tpl->assign("title",$_POST['title']);$tpl->assign("content",stripslashes($_POST['content']));$tpl->assign("time",date("Y-m-d")); $tpl->display("tpl.html"); $this_my_f=ob_get_contents();//讀取緩衝區資料 ob_end_clean();//清空緩衝區資料//------------------------建立檔案夾---------------------------$dir_name =date("Ymd"); //以當前日期,建立應該產生的靜態頁面所要存入的目錄 if (!is_dir("webpage/".$dir_name)) //先判斷是否已經建立了此目錄!無,則先建立此目錄{ mkdir("webpage/".$dir_name);} $filename ="tpl.html"; //-------------------------靜態頁儲存的路徑--------------------if(tohtmlfile_cjjer($filename,$this_my_f)){ echo ("產生頁面成功");}else{ echo ("")}?>function tohtmlfile_cjjer($file_cjjer_name,$file_cjjer_content){ //$dir_name =date("Ymd"); //以當前日期,建立應該產生的靜態頁面所要存入的目錄 //if (!is_dir($dir_name)) //先判斷是否已經建立了此目錄!無,則先建立此目錄 //{ //mkdir($dir_name); //}

if (is_file ($file_cjjer_name)){ @unlink ($file_cjjer_name); } $cjjer_handle = fopen ($file_cjjer_name,"w"); if (!is_writable ($file_cjjer_name)){ return false; } if (!fwrite ($cjjer_handle,$file_cjjer_content)){ return false; } fclose ($cjjer_handle); //關閉指標 return $file_cjjer_name; }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);//產生

smarty產生靜態頁面總結:產生靜態頁面時分離模板的一個方法通常的做法是:讀模數板,用Regex等將模板中的變數替換成我們想要的值才能產生靜態頁面。經高手指點原來SMARTY就有這功能,研究了一下果然很方便,用起來也很簡單,要點如下:ob_start();//開啟緩衝區$smarty->assign(“a”,$a);$smarty->display(”temp.html”);$html_content= ob_get_contents(); //讀取緩衝區的資料ob_end_clean();//關閉緩衝區$htm_content裡頭的東西就是想要的東西了,將它寫入頁面就可以了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.