PHPSmarty页面静态化加分页和缓存控制-创新互联

PHP页面

成都创新互联提供成都网站制作、网站设计、网页设计,品牌网站建设广告投放等致力于企业网站建设与公司网站制作,10多年的网站开发和建站经验,助力企业信息化建设,成功案例突破上1000+,是您实现网站建设的好选择.<?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# }>'> <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>

浏览器页面

PHP Smarty 页面静态化加分页和缓存控制

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

当前名称:PHPSmarty页面静态化加分页和缓存控制-创新互联
网站网址:https://www.cdcxhl.com/article26/djdojg.html

成都网站建设公司_创新互联,为您提供网站内链外贸网站建设小程序开发网站改版网站排名服务器托管

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设