smarty 局部不缓存

来源:互联网 发布:淘宝人群画像在哪看 编辑:程序博客网 时间:2024/05/16 11:46

1、局部不缓存的概念

             页面很复杂,需要设置缓存。但是页面上包含实时性较强的数据(时间和日期,股市行情,天气等),所有在这样的页面上我们将实时性较强的数据部设置缓存,将其他的几乎不变化的内容加上缓存。这就叫做局部不缓存。

2、如何使用局部不缓存

利用insert内建函数可以将smarty模板当中的数据传递给php外置函数去执行,传递过去的函数名要求以insert.funName.php命名,保存在plugins文件夹。
 insert.funName.php文件中的函数名命名格式为:samrty_insert_funName($p){...}  $p的内容是通过smarty当中的insert传递过来的,$p是一关联数组:[insert的属性名]=>属性值

3、实例:

                  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><{ insert name=getStuinfo stuId=$arr.id}></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><{ insert name=getStuinfo stuId=$arr.id}></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

<!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>
<{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 />
成绩:<{ insert name=getStuinfo stuId=$stu_detail[detail].id}><br />
描述:<{$stu_detail[detail].stu_desc}><br />
<{/section}>
</body>
</html>

insert.getStuinfo.php

<?php
function smarty_insert_getStuinfo($p){
    //return $p["stuId"];
    mysql_connect('localhost','root','123');
    mysql_select_db('xsxx');
    $result = mysql_query("select score from stu where id='".$p["stuId"]."'");
    $value = mysql_fetch_assoc($result);
    return $value["score"];
    }


注意:局部不缓存的内容(从数据库中获取时)与之前的从数据库中获取的信息没有一点的联系,他是单独的文件获取的