zoukankan      html  css  js  c++  java
  • 夺命雷公狗---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”);
  • 相关阅读:
    CLR via C#(04) 本是同根生
    CLR via C#(01).NET平台下代码是怎么跑起来的
    CLR via C#(02)基元类型、引用类型、值类型
    Mysql定期自动备份
    Extjs4 图片上传 预览
    inno setup打包应用程序
    bat批处理学习
    localhost/127.0.0.1/本机IP的区别以及端口号
    VirtualBox虚拟机上安装windows7系统
    Linux系统中Oracle11g数据库的安装与验证
  • 原文地址:https://www.cnblogs.com/leigood/p/5033529.html
Copyright © 2011-2022 走看看