smarty局部不缓存

来源:互联网 发布:java取两位小数 编辑:程序博客网 时间:2024/05/16 12:52


insert 所包含的内容不会被缓存,每次调用该模板都会重新执行该函数.
insert(name,assign,script,[var...])
name:插入函数的名称
assign:该属性指定一个变量保存待插入函数输出
script:插入函数前需要先包含的php脚本名称
[var ...]:传递给待插入函数的本地参数
例:
1、例子是在昨天的例子的基础上,今天加了个时间,如果时间也加入缓存,在次访问从缓存调用,时间

一直停留在上次,如果让时间不缓存,下次调用就是当前的时间。自定义的变量调节器是保存的当前时

间。在duohuancun1.tpl中 加入了当前的时间<{insert name=getDate}>
2、今天也在学分上加上了局部不缓存,如果数据库中的学分更改了,缓存中的学分没有更改(因为再次

访问是访问的缓存文件),让学分局部不缓存,数据库中的学分更改,模板中的也能更改。在

duohuancun1.tpl中 学分出加了:<{insert name=getStuinfo pp=$array[stu].c_id}>,学分就被不缓

存,详细信息中的学分也改变了,在duohuancun2.tpl中学分处做的修改:<{insert name=getStuinfo

pp=$stu_details[stu_detail].c_id}>
3、今天多了两个自定义变量调节器的文件

昨天例子的步骤思路:
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号就可以访问不同

的缓存文件(详细昨天的博客)

获取当前的时间,从新访问时还是访问的缓存文件,但时间还是变了,学分也变了,就是因为用了局部缓存

学生详细信息中的学分也改变了


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;//设置缓存时间

//is_cached判断书否被缓存
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
<{config_load file="b.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" 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><{insert name=getStuinfo pp=$array[stu].c_id}><{*$array[stu].c_id就是遍历数组,每一个

c_id,pp传递过去的c_id,当pp=1时,2时,遍历到一个传递一次,name=getStuinfo就得到返回的学分。

getStuinfo是自定义变量调节器*}></td>
<td><a href="duohuancun2.php?id=<{$array[stu].c_id}>">详细信息</a></td>
</tr>

<{/section}>

</table>
<br>
当前时间:<{insert name=getDate}><{*getDate自定义的变量调节器,insert包含的内容不会被缓存

*}>

<br>

</center>

</body>
</html>
3、duohuancun2php

<?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>
学分:<{insert name=getStuinfo pp=$stu_details[stu_detail].c_id}>
<{/section}>
</body>

?>
//下面是自定义变量调节器
5、insert.getDate.php
<?php
function smarty_insert_getDate(){
return date("Y-m-d H:i:s",time());//获得当前时间 
}
?> 
6、insert.getStuinfo.php
<?php

function smarty_insert_getStuinfo($p){
 $link=mysql_connect("localhost","root","123");
    mysql_select_db('xsgl',$link);
    $q='select credit from course where c_id='.$p["pp"] ;
   $result=mysql_query($q);
   $value=mysql_fetch_assoc($result);//返回一个数组,当期传来的id的一条记录
  return  $value['credit'];//返回这条记录里德学分
 
 //return $p["stuName"];
 
 }


?>

原创粉丝点击