夺命雷公狗---Smarty NO:25 缓存控制技术2(完结)

9、缓存集合

列表页或栏目页

select * from goods where kid=1 and page=1

select * from goods where kid=1 and page=2

select * from goods where kid=1 and page=3

由于受到缓存的影响,系统只会记录第一次输出结果,以后都自动显示此页面。

如果想解决以上问题,可以采用如下方案:

$smarty->display(“tpl”, $id1.”|”.$id2);

$smarty -> display(“demo7.html”,$kid.’|’.$page);

10、局部缓存

  • $smarty->assign(“var”, “value”, true); 第三个参数如果为true,代表不缓存
  • {$var nocache=true}:设置某个变量不缓存
  • {nocache}{/nocache}:设置某个范围不缓存

示例1:

$smarty -> assign(‘num’,$num,true);

示例2:

<body>
<h1>点击次数:{$num nocache=true}</h1>
</body>

示例3:

<body>
{nocache}
<h1>{$sql}</h1>
<h1>点击次数{$num}</h1>
{/nocache}
</body>

11、过滤器

1)什么是过滤器

主要是实现对数据进行过滤操作

2)过滤器分类

  • Prefilters 预过滤器
  • Postfilters 后过滤器
  • Output Filters 输出过滤器

3)过滤器的执行流程

tpl源文件 =〉Prefilter =〉编译tpl文件 => Postfilter =>保存到磁盘=> 编译过的php文件执行=〉Output Filters(=〉如果有smarty cache的话,Output Filters的内容会缓存) =>结果输出

示例代码:

4、使用过滤器

在Smarty2.0版本中使用一下方式设置过滤器

$smarty->register_prefilter(“func”);

$smarty->register_postfilter(“func”);

$smarty->register_outputfilter(“func”);

在Smarty3.0中统一使用以下方式实现过滤器:

  • $smarty->registerFilter($type, $callback)
  • $type:定义过滤器的类型
    • pre  预过滤器
    • post 后过滤器
    • output 输出过滤器
  • $callback:自定义函数

示例代码:

<?php
header(“Content-Type:text/html;charset=utf-8″);
require “smarty/Smarty.class.php”;
$smarty = new Smarty();
$title = ‘钓鱼岛是中国的,苍老师是世界的';
$smarty -> assign(‘title’,$title);
function fn($tpl){
$tpl = str_replace(‘苍老师’,’***’,$tpl);
return $tpl;
}
$smarty -> registerFilter(‘output’,’fn’);
$smarty -> display(“demo7.html”);
原文地址:https://www.cnblogs.com/leigood/p/5033529.html