PHP頁面
創(chuàng)新互聯(lián)主營(yíng)六安網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App定制開發(fā),六安h5微信小程序定制開發(fā)搭建,六安網(wǎng)站營(yíng)銷推廣歡迎六安等地區(qū)企業(yè)咨詢
<?php //打開輸出控制緩沖 ob_start(); //新建模板文件 $file = "./cache/newindex{$_GET['page']}.html"; $cachetime = 10; const DSN = 'MySQL:host=localhost;dbname=test'; const DBUSER = 'root'; const DBPWD = 'root'; //模板不存在或者超過緩存時(shí)間才生成緩存模板 if(!file_exists($file) || filemtime($file)+$cachetime<time()){ //創(chuàng)建smarty對(duì)象 require_once './libs/Smarty.class.php'; require_once 'page.class.php'; //定義根目錄 define('ROOT', str_replace("\\", "/",dirname(__FILE__))."/"); //實(shí)例化Smarty類 $smarty=new Smarty(); //設(shè)定定界符 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //設(shè)置為false 定界符號(hào)左右可以有空格 $smarty->auto_literal = false; //添加一個(gè)插件的目錄 //$smarty->setPluginsDir(ROOT."/libs/myplugins/"); //注意添加一個(gè)插件,要把系統(tǒng)默認(rèn)設(shè)置的路徑加入 否則不能使用默認(rèn)系統(tǒng)的插件 $smarty->setPluginsDir(array( ROOT."/libs/plugins/",//系統(tǒng)默認(rèn)設(shè)置的路徑 ROOT."/libs/myplugins/",//自定義的 )); //連接數(shù)據(jù)庫 try{ $pdo = new PDO(DSN, DBUSER,DBPWD); }catch(PDOException $e){ echo "數(shù)據(jù)庫連接失敗:".$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'); //返回輸出緩沖區(qū)的內(nèi)容 $content = ob_get_contents(); //將一個(gè)字符串寫入文件 file_put_contents("./cache/newindex{$_GET['page']}.html", $content); //沖刷出(送出)輸出緩沖區(qū)中的內(nèi)容 ob_flush(); echo "緩存文件不存在,查詢數(shù)據(jù)庫,再生成緩存文件,輸出到瀏覽器"; }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# }>'> <h4><{#title#}></h4> <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>
瀏覽器頁面
網(wǎng)站名稱:PHPSmarty頁面靜態(tài)化加分頁和緩存控制
轉(zhuǎn)載來源:http://muchs.cn/article40/pjpoeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站營(yíng)銷、Google、微信公眾號(hào)、網(wǎng)站設(shè)計(jì)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)