PHP页面

<?php//打开输出控制缓冲ob_start();//新建模板文件$file="./cache/newindex{$_GET['page']}.html";$cachetime=10;constDSN='mysql:host=localhost;dbname=test';constDBUSER='root';constDBPWD='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=newSmarty();//设定定界符$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=newPDO(DSN,DBUSER,DBPWD);}catch(PDOException$e){echo"数据库连接失败:".$e->getMessage();exit;}$query="selectid,username,password,emailfromuserslimit3";$stmt=$pdo->prepare($query);$stmt->execute();$users=$stmt->fetchAll(PDO::FETCH_ASSOC);$smarty->assign('users',$users);//var_dump($users);$query="descusers";$stmt=$pdo->prepare($query);$stmt->execute();$tdname=$stmt->fetchAll(PDO::FETCH_COLUMN);//var_dump($tdname);$page=newPage(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><metahttp-equiv="content-type"content="text/html;charset=utf-8"/></head><styletype="text/css"></style><scripttype="text/javascript">functionabc(){}</script><{config_loadfile="../config/my.conf"section="index"}><bodybgcolor='<{#bgcolor#}>'><h4><{#title#}></h4><tableborder="1"width="800"align="center"><caption>用户信息表</caption><thalign="center">index</th><thalign="center">iteration</th><{foreach$tdnameas$val}><thalign="center"><{$val}></th><{/foreach}><{sectionname="one"loop=$users}><{if$smarty.section.one.first}><trbgcolor="red"align="center"><{elseif$smarty.section.one.last}><trbgcolor="yellow"align="center"><{elseif$smarty.section.one.iterationiseven}><trbgcolor="pink"align="center"><{else}><trbgcolor="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><tdcolspan="6"align="center"><{$fpage}></td></tr></tfoot></table></body>

浏览器页面