首先先要把需要靜態化的內容填充到模版中去
代碼如下:#eg.這個是靜態化首頁的 function staticIndex(){ $newslist = $article->getArticles(null,54,'DESC',1,6,false,1,2,'',0,0,1); if($newslist){ foreach($newslist as $k=>$v){ $newslist[$k]['title_all'] = $v['title']; $newslist[$k]['title'] = cutstr($v['title'],36,'…'); } $smarty->assign('newslist',$newslist); } $content = '';$content = $smarty->fetch('index.html',true);//這是Smarty內建的產生靜態頁面的函數$static_name = ROOT_PATH.'index.html';//這是產生靜態頁面當前的路徑檔案fopen($static_name,'a');//開啟這個檔案@file_put_contents($static_name,$content);//最後寫進去return true;} //靜態化列表頁,按類別不同經行靜態化 function staticContent(){//需要靜態話的條數 $ids = array();//擷取所有的內容 $ids = $this->getListIds();//這個方法擷取所有的內容,下面紅字部分對應它的方法 foreach($ids as $k=>$value){ //echo $value['catid']; if(!file_exists(ROOT_PATH.'demo/')){//判斷根目錄下面有沒有這個檔案夾,如果沒有則建立demo這個檔案夾 mkdir(ROOT_PATH.'demo/'); } if(!file_exists(ROOT_PATH.'demo/'.$value['catid'])){//判斷這個檔案夾下面有沒有對應的類別檔案夾 mkdir(ROOT_PATH.'demo/'.$value['catid']); } $html_content = $this->getDemoContent($value['demoid']); $static_name = ROOT_PATH.'demo/'.$value['catid'].'/'.$value['demoid'].'.html'; fopen($static_name,'a'); @file_put_contents($static_name,$html_content); } return true; } //拿出需要靜態化的頁面IDfunction getListIds(){ $sql = "select * from {$this->tablepre}demo order by demoid asc"; $rs = $this->db->getAll($sql); if($rs){ return $rs; }else{ return false; }} //content單頁靜態化function getDemoContent($id){ global $smarty,$view_templates,$admin_templates; loadModel(array('demo')); $demo = new demo(); $content = ''; $smarty->template_dir = ROOT_PATH.$view_templates; $getMobanOne = $this->getMobanDetail($id); $mobandetail = $demo->MobanList($id); foreach($mobandetail as $k=>$v){ $smarty->assign($k,$v); } $this->catid = $getMobanOne['catid']; $smarty->assign('pre_title',$mobandetail['membername']); $smarty->assign('mobandetail',$mobandetail); $content = $smarty->fetch('demo_show.html',true); $smarty->template_dir = ROOT_PATH.$view_templates; return $content;} 以前用的那個產生靜態頁面每次都要執行PHP檔案,然後才能產生,現在我寫的這個就是由人控制的,想產生就產生,很方便,希望能給大家帶來方便,祝大家工作愉快。