init.smarty.php
<?phpdefine("ROOT", ".");//解決問題:Warning: strftime() [function.strftime]:date_default_timezone_set("Asia/Shanghai");include ROOT."/libs/Smarty.class.php";$tpl = new Smarty();//smarty初始化$tpl->template_dir=ROOT."/templates/";$tpl->compile_dir=ROOT."/templates_c/";//設定檔位置$tpl->config_dir=ROOT."/configs/";$tpl->caching=0;//快取檔案位置;開發階段不要開啟,運行階段再開啟$tpl->cache_dir = ROOT."/cache/"; //設定緩衝目錄$tpl->cache_lifetime=60;$tpl->left_delimiter="<!--{";$tpl->right_delimiter="}-->";?>
mysmarty.php
<?php/** * * 以下所有使用變數都要基於設定的首碼和尾碼<!--{}--> * smarty的緩衝類似於網頁靜態化(使用smarty緩衝非常方便) * 1.需要開啟緩衝 * 2.指定一下緩衝的時間 * 3.指定快取檔案的儲存位置 * 4.清除緩衝 * $tpl->clear_cache["模板","cacheID"] * $tpl->clear_all_cache(); * * * 注意: * 1.一個模板只能有一個快取檔案 * 2.如果一個模板的多個文章,則需要每個文章進行緩衝 * $tpl->display("mysmarty.html",cacheid); * cacheid最好使用:$_SERVER["REQUEST_URI"]地址欄來進行儲存 * 3.一定要使用smarty函數isCached來判斷是否存在緩衝 * 4.將使用者你好,和發表評論時不進行緩衝的設定(局部不緩衝) * 將資料寫在非緩衝之外 * 方法一:php註冊方式 * $tpl->registerPlugin("block", "nocache", "ncache",FALSE); * 註冊塊函數 * function ncache($param,$content){ * return $content; * } * 方法二:寫塊檔案 * //註冊塊需要修改Smarty的編譯檔案 * //1.添加塊檔案 * //2.修改smartyCompile檔案(700行左右) * //if($tag_command=="nocache") * //$this->_plugins['block'][$tag_command] = array($plugin_func,null,null,null,false); * //else * //$this->_plugins['block'][$tag_command] = array($plugin_func,null,null,null,true); *///如果檔案載入失敗require會停止繼續解析php;而include則會繼續向下執行require 'init.smarty.php';require 'page.class.php';//判斷是否存在快取檔案if(!$tpl->isCached("mysmarty.html",$_SERVER["REQUEST_URI"])){$title="這是一個文字標題";$mysqli = new mysqli("localhost","root","root","hibernate");$result = $mysqli->query("select id,name,price from users");$page = new Page($result->num_rows,5);$result = $mysqli->query("select id,name,price from users {$page->limit}");$data=array();while ($row=$result->fetch_assoc()){$data[]=$row;}$tpl->assign("title",$title);$tpl->assign("secT",$data);$tpl->assign("fpage",$page->fpage());echo date("Y-m-d H:i:s");}//設定為局部緩衝$tpl->assign("date",date("Y-m-d H:i:s"));$tpl->registerPlugin("block", "nocache", "ncache",FALSE);/** * 註冊塊函數 * @param unknown_type $param * @param unknown_type $content */function ncache($param,$content){return $content;}//模板檔案名稱可以隨便定義:比如:mysmarty.tpl只有內容是html就可以了//第二個參數,每變化一個值就存一份不同的緩衝$tpl->display("mysmarty.html",$_SERVER["REQUEST_URI"]);?>
page.class.php
<?phpclass Page {private $total; //資料表中總記錄數private $listRows; //每頁顯示行數private $limit;private $uri;private $pageNum; //頁數private $config=array('header'=>"records", "prev"=>"prev", "next"=>"next", "first"=>"first", "last"=>"last");private $listNum=8;/* * $total * $listRows */public function __construct($total, $listRows=10, $pa=""){$this->total=$total;$this->listRows=$listRows;$this->uri=$this->getUri($pa);$this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;$this->pageNum=ceil($this->total/$this->listRows);$this->limit=$this->setLimit();}private function setLimit(){return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}";}private function getUri($pa){$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa;$parse=parse_url($url);if(isset($parse["query"])){parse_str($parse['query'],$params);unset($params["page"]);$url=$parse['path'].'?'.http_build_query($params);}return $url;}public function __get($args){if($args=="limit")return $this->limit;elsereturn null;}private function start(){if($this->total==0)return 0;elsereturn ($this->page-1)*$this->listRows+1;}private function end(){return min($this->page*$this->listRows,$this->total);}private function first(){if($this->page==1)@$html.='';else@$html.=" <a href='{$this->uri}&page=1'>{$this->config["first"]}</a> ";return $html;}private function prev(){if($this->page==1)@$html.='';else@$html.=" <a href='{$this->uri}&page=".($this->page-1)."'>{$this->config["prev"]}</a> ";return $html;}private function pageList(){$linkPage="";$inum=floor($this->listNum/2);for($i=$inum; $i>=1; $i--){$page=$this->page-$i;if($page<1)continue;$linkPage.=" <a href='{$this->uri}&page={$page}'>{$page}</a> ";}$linkPage.=" {$this->page} ";for($i=1; $i<=$inum; $i++){$page=$this->page+$i;if($page<=$this->pageNum)$linkPage.=" <a href='{$this->uri}&page={$page}'>{$page}</a> ";elsebreak;}return $linkPage;}private function next(){if($this->page==$this->pageNum)@$html.='';else@$html.=" <a href='{$this->uri}&page=".($this->page+1)."'>{$this->config["next"]}</a> ";return $html;}private function last(){if($this->page==$this->pageNum)@$html.='';else@$html.=" <a href='{$this->uri}&page=".($this->pageNum)."'>{$this->config["last"]}</a> ";return $html;}private function goPage(){return ' <input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> ';}function fpage($display=array(0,1,2,3,4,5,6,7,8)){$html[0]=" total:<b>{$this->total}</b>{$this->config["header"]} ";$html[1]=" pageSize:<b>".($this->end()-$this->start()+1)."</b>,currentSize:<b>{$this->start()}-{$this->end()}</b> ";$html[2]=" <b>{$this->page}/{$this->pageNum}</b> ";$html[3]=$this->first();$html[4]=$this->prev();$html[5]=$this->pageList();$html[6]=$this->next();$html[7]=$this->last();$html[8]=$this->goPage();$fpage='';foreach($display as $index){$fpage.=$html[$index];}return $fpage;}}
mysmarty.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title><!--{$title}--></title></head><body><!--{nocache}--><!--{$date}--><!--{/nocache}--><tableborder="1" width="800"><tr><th>行號</th><th>索引</th><th>ID</th><th>名稱</th><th>價格</th></tr><!--{section loop=$secT name="ls"}--><tr><td><!--{$smarty.section.ls.rownum}--></td><td><!--{$smarty.section.ls.index}--></td><td><!--{$secT[ls].id}--></td><td><!--{$secT[ls].name}--></td><td><!--{$secT[ls].price}--></td></tr><!--{sectionelse}--><tr><td>數組為空白,或沒有分配</td></tr><!--{/section}--><tr><td colspan="5"><!--{$fpage}--></td></tr></table><br></body></html>