1、单页面多缓存的概念
为了处理同样的显示界面,不同的显示内容。内容有url参数决定。同一个模板可以生成多个缓存文件(有多少中内容显示就有多少个缓存文件),多个缓存缓存文件用display("*.tpl",$_GET["id"]);在PHP中可以用is_cached判断模板是否被缓存,is_cached格式和display完全一样。
注意:is_cached()函数 在指定模板的缓存存在时返回真。只有在缓存设置为真时才可用
display(参数1:文件资源的路径或类型,参数2:缓存号)
实例:
stu_info.php
<?php
$link = mysql_connect('localhost','root','123');
mysql_select_db('xsxx');
mysql_query("set names utf8");
include "../../libs/Smarty.Class.php";
$smarty=new Smarty();
$smarty->template_dir="../../demo/templates";
$smarty->compile_dir="../../demo/templates_c";
$smarty->config_dir="../../demo/config";
$smarty->cache_dir="../../demo/cache";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->caching=1;
if(!$smarty->is_cached('stu_info_dan.tpl')){
$result = mysql_query("select * from stu");
$array = array();
while($row = mysql_fetch_assoc($result)){
$array[]=$row;
}
//var_dump($array);
$smarty->assign("array",$array);
echo "当前文件没有缓存……";
}
$smarty->display("stu_info_dan.tpl");
stu_info.dan.tpl
<{config_load file="stu_info.conf"}>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生基本信息</title>
</head>
<body>
<center>
<table border="1">
<caption><font color="#000000" size="+2" style=" font-family:'楷体';">学生基本信息</font></caption>
<tr bgcolor="<{#table_bgcolor#}>">
<th>学号</th><th>姓名</th><th>出生日期</th><th>成绩</th><th>操作</th>
</tr>
<{foreach from=$array item=arr key=key_s name="color"}>
<{if $smarty.foreach.color.iteration%2==0}>
<tr bgcolor="<{#head_color1#}>">
<td><{$arr.id}></td>
<td><{$arr.name}></td>
<td><{$arr.birthday}></td>
<td><{ $arr.score}></td>
<td><a href="details.php?id=<{$arr.id}>">详细信息</a></td>
</tr>
<{else}>
<tr bgcolor="<{#head_color2#}>">
<td><{$arr.id}></td>
<td><{$arr.name}></td>
<td><{$arr.birthday}></td>
<td><{ $arr.score}></td>
<td><a href="details.php?id=<{$arr.id}>">详细信息</a></td>
</tr>
<{/if}>
<{/foreach}>
</table>
<br />
</center>
</body>
</html>
details.php
<?php
include "connect_stu.php";
include "../../libs/Smarty.Class.php";
$smarty=new Smarty();
$smarty->template_dir="../../demo/templates";
$smarty->compile_dir="../../demo/templates_c";
$smarty->config_dir="../../demo/config";
$smarty->cache_dir="../../demo/cache";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->caching=1;
if(!$smarty->is_cached('stu_detail_dan.tpl',$_GET['id'])){
$result = mysql_query("select * from stu where id='".$_GET['id']."'");
$stu_detail = array();
while($row = mysql_fetch_assoc($result)){
$stu_detail[]=$row;
}
//var_dump($array);
$smarty->assign("stu_detail",$stu_detail);
echo "当前文件没有缓存……";
}
$smarty->display("stu_detail_dan.tpl",$_GET['id']);
stu_detail_dan.tpl
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生详细信息</title>
</head>
<body>
<{section name=detail loop=$stu_detail}>
<{$stu_detail[detail].name}>的详细信息
<hr />
学号:<{$stu_detail[detail].id}><br />
姓名:<{$stu_detail[detail].name}><br />
出生日期:<{$stu_detail[detail].birthday}><br />
成绩:<{ $stu_detail[detail].score}><br />
描述:<{$stu_detail[detail].stu_desc}><br />
<{/section}>
</body>