用php生成靜態(tài)文件的方式很簡(jiǎn)單,就是用fopen()方法,fwrite(),fclose(),就好了,下面是php文檔中fopen中mode值的說明:
然后我們有一個(gè)需求就是在smarty模版引擎中點(diǎn)擊一個(gè)按鈕生成一個(gè)html的文件,內(nèi)容是從數(shù)據(jù)庫(kù)讀出來的一串循環(huán)的、有層級(jí)的數(shù)據(jù),這時(shí)候我們應(yīng)該怎么做了?
用smarty生成靜態(tài)html文件的關(guān)鍵就是用緩存技術(shù),開啟緩沖,用display或者fetch向前臺(tái)傳輸數(shù)據(jù)的時(shí)候其實(shí)不會(huì)顯示在view上,這時(shí)候打開文件,寫入文件,就生成好了一個(gè)靜態(tài)文件。
我們看2個(gè)實(shí)例:
1、
public function index() { $this->cismarty->caching = true; ob_start(); $title = "title"; $shopName = "穿越火線"; $model = '<li><h3>{#$shopName#}</h3></li>'; $outfilename = ".html"; $this->cismarty->assign("title", $title); $this->cismarty->assign("model", $model); $this->cismarty->assign("shopName", $shopName); $this->cismarty->display("ppms/smarty.html"); $str = ob_get_contents(); $fp = @fopen($outfilename, 'w'); if (!$fp) { Show_Error_Message( ERROR_WRITE_FILE ); } fwrite($fp, $str); fclose($fp); ob_end_clean(); }
2、
function makeHtml(){ ob_start(); $this->cismarty->assign("title","Hello World!"); $content = $this->cismarty->fetch("ppms/smarty.html"); //這里的 fetch() 就是獲取輸出內(nèi)容的函數(shù),現(xiàn)在$content變量里面,就是要顯示的內(nèi)容了 $fp = fopen("0001.html", "w"); fwrite($fp, $content); fclose($fp); }
上面兩種方式是常用的,第一種用display方法,用$str = ob_get_contents();得到向前臺(tái)輸出的內(nèi)容,第二種用fetch直接獲取向前臺(tái)輸出的內(nèi)容(兩種都不會(huì)真正地展示出來中)。然后寫入到文件中,因?yàn)橛?ldquo;w”的方式,沒有這個(gè)文件就會(huì)新建一個(gè)。成功~~~
PHP編程鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選