1、smarty单页面,多缓存
display生成缓存文件。缓存文件只保存模板中的内容,php中的内容是不会保存在缓存文件中的。
为了处理同样的显示界面,不同的显示内容,内容由url参数决定,同一个模板可以生成多个缓存文件(有多少内容显示就有多少个缓存文件)多个缓存文件用display(“*tpl”,$GET["id"]);在php中可以用is_cached判断模板是否被缓存,is_cached和display一样
例:
步骤思路:
1、从数据库中读取内容(一个表),读出之后把内容写成二维数组形式(duohuancun.php),
2、在模板(duohuancun1.tpl)中把它以表格的形式读出来,在读出的表格中有增加一列详细信息,当你点击详细信息时(用超链接<a>)会跳转到duohuancun2.php中,在超链接地址(<ahref="duohuancun2.php?id=<{$array[stu].c_id}>">)会设置一个id,值是当前的数据库中的c_id号(学号)。
3、在duohuancun2.php中,会连接数据库查看数据库中的内容,id就是你地址中传来的id("select * from course where c_id=".$_GET["id"];)从数据库中读出后写成二维数组的形式,duohuancun2.tpl模板中用section遍历出来。
4、当你创建缓存时,你预览一次缓存生成,下次再访问时,就直接访问缓存文件,导致你查看一个人的详细信息后,在查看其它人的详细信息时,内容都是你第一次看的信息,所以用到单页面多缓存,给同一个模板生成多个缓存文件。在duohuancun2.php中$smarty->display("duohuancun2.tpl",$_GET["id"]),它就设置了生成多个缓存文件,每个缓存文件名字前都有一个id号,根据id号就可以访问不同的缓存文件
当你在此访问时,就直接访问的缓存文件,php中的内容是保存不到缓存文件中的,所以不显示没有设置缓存,它还与下文duohuancun1.php中的if()判断有关。
点击查看每个人的详细信息时就用到多缓存。
缓存没有被设置。。消失是因为在次访问时访问的是缓存文件,php的内容不能保存到缓存文件中,与duohuancun2.php中if设置有关
利用多缓存文件就可以查看其它的信息,一个模板多个缓存文件
缓存文见名字前面都有一个id号,是同过$smarty->display("duohuancun2.tpl",$_GET["id"])设置的
1、duohuancun1.php
<?php
include("../libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="../demo/templates";
$smarty->compile_dir="../demo/tempaltes_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->config_dir="../demo/config";//配置文件存放路径
$smarty->caching=true;//开启缓存
$smarty->cache_dir="../demo/cache";//缓存文件存放的路径
//$smarty->cache_lifetime=20;//设置缓存时间
if(!$smarty->is_cached("duohuancun1.tpl")){//第一遍执行时还没用生成缓存,判段正确,往下执行,输出没有设置缓存,然后display生成缓存,等在执行时(或刷新时)缓存已经生成,if这判断错误,不会执行下面的代码,也不会显示没有设置缓存,就会节省执行这段代码的时间
$link=mysql_connect("localhost","root","123");
mysql_select_db('xsgl',$link);
$result=mysql_query("select * from course");
$array=array();
$i=0;
while($row=mysql_fetch_assoc($result))
{
$array[$i]=$row;
$i++;
}
//print_r($aray);//以二维数组的形式
$smarty->assign("array",$array);
echo "没有设置缓存。。。";
}
$smarty->display("duohuancun1.tpl");//display生成缓存文件。缓存文件只是保存模板的内容,php中的内容不会保存在模板中
?>
2、duohuancun1.tpl
<body>
<center>
<table border="1" bgcolor="<{#table_bgcolor#}>">
<tr bgcolor="<{#head_color#}>">
<th>c_id</th><th>c_name</th><th>学期</th><th>课时</th><th>学分</th><th>操作</th>
<{section name=stu loop=$array}>
<tr bgcolor="<{#row_color#}>">
<td><{$array[stu].c_id}></td>
<td><{$array[stu].c_name}></td>
<td><{$array[stu].kkxq}></td>
<td><{$array[stu].clsaa_hour}></td>
<td><{$array[stu].credit}></td>
<td><a href="duohuancun2.php?id=<{$array[stu].c_id}>">详细信息</a></td>
</tr>
<{/section}>
</table>
</center>
3、duohuancun2.php
<?php
include("../libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="../demo/templates";
$smarty->compile_dir="../demo/tempaltes_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->config_dir="../demo/config";//配置文件存放路径
$smarty->caching=true;//开启缓存
$smarty->cache_dir="../demo/cache";//缓存文件存放的路径
$link=mysql_connect("localhost","root","123");
mysql_select_db('xsgl',$link);
if(!$smarty->is_cached("duohuancun2.tpl",$_GET["id"])){//第一遍执行时还没用生成缓存,判段正确,往下执行,输出没有设置缓存,然后display生成缓存,等在执行时(或刷新时)缓存已经生成,if这判断错误,不会执行下面的代码,也不会显示没有设置缓存,就会节省执行这段代码的时间。因为你给缓存文件加了个id号,(缓存文件名上有id号)而你访问它时如果没有$_GET["id"],你访问的地址上就没有这个id号,所以你在刷新或重新访问时,就是假的,就会在执行if这句话,缓存没有设置这句话就一致存在
$q="select * from course where c_id=".$_GET["id"];
$result=mysql_query($q);
$array=array();
$i=0;
while ($row = mysql_fetch_assoc($result)){
$array[$i]=$row;
$i++;
}
$smarty->assign("stu_details",$array);
echo "缓存没有被设置。。";
}
$smarty->display("duohuancun2.tpl",$_GET["id"]);//display生成缓存文件。缓存文件只是保存模板的内容,php中的内容不会保存在模板中。生成多个缓存文件,在缓存文件的名字开始处会有$_GET["id"],
?>
4、duohuancun2.tpl
<body>
<{section name=stu_detail loop=$stu_details}>
<h1><{$stu_details[stu_detail].c_name}>的详细信息</h1>
<hr>
学号:<{$stu_details[stu_detail].c_id}><br>
姓名:<{$stu_details[stu_detail].c_name}><br>
学期:<{$stu_details[stu_detail].kkxq}><br>
课时:<{$stu_details[stu_detail].clsaa_hour}><br>
学分:<{$stu_details[stu_detail].credit}>
<{/section}>
</body>