標籤:php smarty 頁面靜態化加分頁和緩衝控制
PHP頁面
<?php //開啟輸出控制緩衝ob_start();//建立模板檔案$file = "./cache/newindex{$_GET[‘page‘]}.html";$cachetime = 10;const DSN = ‘mysql:host=localhost;dbname=test‘;const DBUSER = ‘root‘;const DBPWD = ‘root‘;//模板不存在或者超過緩衝時間才產生緩衝模板if(!file_exists($file) || filemtime($file)+$cachetime<time()){//建立smarty對象require_once ‘./libs/Smarty.class.php‘;require_once ‘page.class.php‘;//定義根目錄define(‘ROOT‘, str_replace("\\", "/",dirname(__FILE__))."/");//執行個體化Smarty類$smarty=new Smarty();//設定定界符$smarty->left_delimiter="<{";$smarty->right_delimiter="}>";//設定為false 定界符號左右可以有空格$smarty->auto_literal = false;//添加一個外掛程式的目錄//$smarty->setPluginsDir(ROOT."/libs/myplugins/");//注意添加一個外掛程式,要把系統預設設定的路徑加入 否則不能使用預設系統的外掛程式$smarty->setPluginsDir(array( ROOT."/libs/plugins/",//系統預設設定的路徑 ROOT."/libs/myplugins/",//自訂的));//串連資料庫try{ $pdo = new PDO(DSN, DBUSER,DBPWD);}catch(PDOException $e){ echo "資料庫連接失敗:".$e->getMessage(); exit;}$query = "select id, username, password,email from users limit 3";$stmt = $pdo->prepare($query);$stmt ->execute();$users = $stmt->fetchAll(PDO::FETCH_ASSOC);$smarty->assign(‘users‘,$users);//var_dump($users);$query = "desc users";$stmt = $pdo->prepare($query);$stmt ->execute();$tdname = $stmt->fetchAll(PDO::FETCH_COLUMN);//var_dump($tdname);$page = new Page(20, 2);$smarty->assign(‘tdname‘,$tdname);$smarty->assign(‘fpage‘,$page->fpage());//變數輸出$smarty->display(‘hello.tpl‘);//返回輸出緩衝區的內容$content = ob_get_contents();//將一個字串寫入檔案file_put_contents("./cache/newindex{$_GET[‘page‘]}.html", $content);//沖刷出(送出)輸出緩衝區中的內容ob_flush();echo "快取檔案不存在,查詢資料庫,再產生快取檔案,輸出到瀏覽器";}else{ include $file; echo "直接載入快取檔案";}?>
模板tpl頁面
<head><meta http-equiv="content-type" content="text/html;charset=utf-8"/></head><style type="text/css"></style><script type="text/javascript"> function abc(){ }</script><{config_load file="../config/my.conf " section="index"}><body bgcolor=‘<{ #bgcolor# }>‘><h3><{#title#}></h3><table border="1" width="800" align="center"><caption>使用者資訊表</caption> <th align="center">index</th> <th align="center">iteration</th> <{foreach $tdname as $val}> <th align="center"><{$val}></th> <{/foreach}> <{section name="one" loop=$users }> <{if $smarty.section.one.first}> <tr bgcolor="red" align="center"> <{elseif $smarty.section.one.last}> <tr bgcolor="yellow" align="center"> <{elseif $smarty.section.one.iteration is even}> <tr bgcolor="pink" align="center"> <{else}> <tr bgcolor="green" align="center"> <{/if}> <td><{$smarty.section.one.index}></td> <td><{$smarty.section.one.iteration}></td> <td><{$users[one].id}></td> <td><{$users[one].username}></td> <td><{$users[one].password}></td> <td><{$users[one].email}></td> </tr> <{sectionelse}> 沒有使用者查詢出來! <{/section}> <tfoot><tr><td colspan="6" align="center"> <{$fpage}> </td></tr></tfoot></table></body>
瀏覽器頁面
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/86/9F/wKioL1fFfpOjAriAAAA6bv1SNIo927.png-wh_500x0-wm_3-wmp_4-s_867193092.png" title="Firefox_2016-08-30T12-38-34.786Z.png" alt="wKioL1fFfpOjAriAAAA6bv1SNIo927.png-wh_50" />
本文出自 “津沙港灣” 部落格,請務必保留此出處http://11410485.blog.51cto.com/11400485/1844499
PHP Smarty 頁面靜態化加分頁和緩衝控制