smarty的缓冲

首先在main文件夹里面建一个文件 huancun.php   然后在template文件夹里面建一个文件 huancun.html  

huancun.php中的内容为:

require("../init.inc.php");
    require("../DBDA.class.php");;

    $db = new DBDA();

    $sql="select * from nation ";
    $arr=$db->Query($sql);

    $smarty->assign("arr",$arr);
    $smarty->display("huancun.html");

huancun.html中的内容为:

<table width="100%" cellpadding="0" cellspacing="0" border="1">
    <tr>
        <td>代号</td>
        <td>名称</td>
        <td>操作</td>
    </tr>
    <{foreach $arr as $v}>
    <tr>
        <td><{$v[0]}></td>
        <td><{$v[1]}></td>
        <td><a href="shanchu.php?code=<{$v[0]}>">删除</a>
            <a href="xiugai.php?code=<{$v[0]}>">修改</a> </td>
    </tr>
    <{/foreach}>

</table>

<a href="tianjia.php">添加</a>

在这个基础上 我要建立一个缓存文件,那么就要在huncun.php中做修改了,同时也在其基础上做了分页的缓存处理

<?php
//定义当前位置所在的页数
$p=1;
if(!empty($_GET["page"]))
{
    $p=$_GET["page"];
}

//定义一个缓存页面的位置,通常放到cache文件中
$filename="../cache/huancunpage{$p}.html";

//定义缓存的时间   filemname 指的是缓存文件的修改时间
$tj=10;

//判断当前页面是否需要缓存
if(file_exists($filename)&&filemtime($filename)+$tj>=time())
{
    //如果存在缓存页面 就显示缓存
    include($filename);
}
else{
    //重新生成缓存
    ob_start();//开启缓存

    require("../init.inc.php");
    require("../DBDA.class.php");;
    require("../page.class.php");

    $db = new DBDA();
    $ztss = "select count(*) from nation";
    $zts = $db->StrQuery($ztss);

    $page = new Page($zts,5);

    $sql="select * from nation ".$page->limit;//注意一下nation后面的空格 这个空格是必须要有的
    $arr=$db->Query($sql);

    $smarty->assign("page",$page->fpage());
    $smarty->assign("arr",$arr);
    $smarty->display("huancun.html");

    $str=ob_get_contents();//从内存中获取内容
    file_put_contents($filename,$str);//把获取到的内容放到缓存文件里边
    ob_flush();//清除缓存

    echo"######"; //这里是为了测试是不是有缓存

}
时间: 2024-07-31 15:15:04

smarty的缓冲的相关文章

PHP Smarty 页面静态化加分页和缓存控制

PHP页面 <?php  //打开输出控制缓冲 ob_start(); //新建模板文件 $file = "./cache/newindex{$_GET['page']}.html"; $cachetime = 10; const  DSN = 'mysql:host=localhost;dbname=test'; const   DBUSER = 'root'; const   DBPWD     = 'root'; //模板不存在或者超过缓存时间才生成缓存模板 if(!fil

smarty实现缓存

首先需要在mySmarty中添加配置信息,开启缓存,设置缓存文件存放目录,设置缓存时间缓存可以实现减少访问数据库,减轻数据库压力,访问一次数据库,形成静态页面,下次直接调用这个页面,也可以用nocache标签实现局部不缓存 在mysmarty类中添加的语句 $this->cache_dir='./cache';//缓存文件存放目录 //开启缓存 $this->caching=true; //配置缓存的生命周期 $this->cache_lifetime=3600;//单位是秒 控制页面0

smarty的常用方法(二)の缓存

一.smarty缓存的应用(页面缓存) smarty缓存的用法: 1.开启 2.配置缓存的生命周期 3.判断是否缓存并是否从数据库取数据 4.输出 例如 //开启缓存$smarty->caching=true; //设置一个缓冲的生命周期 $smarty->cache_lifetime=3600; //设置缓存目录,用于存储缓存文件 $smarty->cache_dir='./cache'; if(!$smarty->isCached('01.html')){ echo '是否走了

smarty后台文件常用方法及说明

<?php require 'smarty.inc.php'; global $_smarty;//访问引入文件的变量 //分配变量 #普通变量 #该函数还有第三个参数,设置是否启用缓冲,如果为true, #变量将不被缓冲 $_smarty->assign('title','虎哥开始学习smarty了');//给前端基本变量赋值 // $_smarty->assignByRef():同上,只是按照引用赋值 #数组 $arr1= array('协调','毛姐');//索引数组 $arr2=

smarty前端常用标签

{* {extends file='blockparent.tpl'} *} {*必须放在模板的第一行,如果要用子模板来扩展父模板,那么它只能有{block}的区域任何其他模板的内容将被忽略*} {config_load file='config.conf'}{*载入配置文件*} <html> <head> <meta charset='utf-8' /> <style type="text/css"> .nav{ margin-left

【转】php缓冲 output_buffering和ob_start

原文: http://blog.csdn.net/21aspnet/article/details/7389427 php缓冲 output_buffering和ob_start buffer buffer是一个内存地址空间,Linux系统默认大小一般为4096(4kb),即一个内存页.主要用于存储速度不同步的设备或者优先级不同的设备之间传办理数据的区域.通过buffer,可以使进程这间的相互等待变少.这里说一个通俗一点的例子,你打开文本编辑器编辑一个文件的时候,你每输入一个字符,操作系统并不会

浅谈libevent的使用--事件和数据缓冲

首先在学习libevent库的使用前,我们还要从基本的了解开始,已经熟悉了epoll以及reactor,然后从event_base学习,依次学习事件event.数据缓冲Bufferevent和数据封装evBuffer等,再结合具体的几个实例来了解libevent库的一些基本使用,有助于我们理解它的一些内部实现(由于之前我已经写过一篇epoll反应堆模型的,所以这里就不再介绍,直接从event_base开始介绍). libevent下载与安装: 在官网上找到 libevent-2.0.22-sta

smarty变量调节器如何使用

smarty变量调节器是用来改变显示的值,不改变变量的值.这里给大家总结了一下,希望对大家的smarty学习有参考意义. 1.capitalize[首字符大写]  调用方法: <?php$smarty->assign('articleTitle', 'next x-men film, x3, delayed.');?> 调用方法: {$articleTitle} {$articleTitle|capitalize} {$articleTitle|capitalize:true} 实现结果

Smarty 函数讲解

这里给大家总结了几种Smarty 函数并分别详细讲解了.如果你正在学习Smarty  ,希望这篇文章对你有用. html_checkboxes 自定义函数 html_checkboxes 根据给定的数据创建复选按钮组. 该函数可以指定哪些元素被选定. 要么必须指定 values 和 ouput 属性,要么指定 options 替代. 所有的输出与 XHTML 兼容 html_checkbox用来用给定的数据创建checkbox.name表示checkbox的名称,values表示checkbox