php smarty模版引擎中的缓存应用_php模板

1,Smarty缓存的配置:
$smarty->cache-dir="目录名"; //创建缓存目录名
$smarty->caching=true; //开启缓存,为false的时候缓存无效
$smarty->cache_lifetime=60; //缓存时间,单位是秒
2,Smarty缓存的使用与清除
$marty->display("cache.tpl",cache_id); //创建带ID的缓存
$marty->clear_all_cache(); //清楚所有缓存
$marty->clear_cache("index.php"); //清楚index.php中的缓存
$marty->clear_cache("index.php',cache_id); //清楚index.php中指定ID的缓存
3,Smarty的局部缓存
第一个: insert_函数默认是不缓存,这个属性是不能修改
使用方法:例子
index.php中,
function insert_get_time(){
return date("Y-m-d H:m:s");
}
index.html中,
{insert name="get_time"}

第二个: smarty_block
定义一个block:smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名
注册block:$smarty->register_block('name', 'smarty_block_name', false); //第三参数false表示该区域不被缓存
模板写法:{name}内容{/name}
写成block插件:
1)定义一件插件函数:block.cacheless.php,放在smarty的plugins目录
block.cacheless.php的内容如下:
<?php
function smarty_block_cacheless($param, $content, &$smarty) {
return $content;
}
?>
2) 编写程序及模板
示例程序:testCacheLess.php

复制代码 代码如下:

<?php
include('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching=true;
$smarty->cache_lifetime = 6;
$smarty->display('cache.tpl');
?>

所用的模板:cache.tpl
已经缓存的:{$smarty.now}<br>
{cacheless}
没有缓存的:{$smarty.now}
{/cacheless}
4自定义缓存
设置cache_handler_func使用自定义的函数处理缓存
如:
$smarty->cache_handler_func = "myCache";
function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){
}
该函数的一般是根椐$action来判断缓存当前操作:
switch($action){
case "read"://读取缓存内容
case "write"://写入缓存
case "clear"://清空
}
一般使用md5($tpl_file.$cache_id.$compile_id)作为唯一的cache_id
如果需要,可使用gzcompress和gzuncompress来压缩和解压

时间: 2024-09-15 02:41:52

php smarty模版引擎中的缓存应用_php模板的相关文章

php smarty模版引擎中的缓存应用_php技巧

1,Smarty缓存的配置: 复制代码 代码如下: $smarty->cache-dir="目录名"; //创建缓存目录名 $smarty->caching=true; //开启缓存,为false的时候缓存无效 $smarty->cache_lifetime=60; //缓存时间,单位是秒 2,Smarty缓存的使用与清除 复制代码 代码如下: $marty->display("cache.tpl",cache_id); //创建带ID的缓存

php smarty模版引擎中变量操作符及使用方法_php模板

smarty常用的20个变量操作符 * 使用语法:{变量名|操作符:} * capitalize ---首字母大写 * count_characters ---计算字符数 * cat ---连接字符串 * count_paragraphs ---计算段落数 * count_sentences ---计算句数 * count_words ---计算词数 * date_format ---时间格式 * default ---默认 * escape ---转码 * indent ---缩进 * low

onethink thinkphp整合smarty模版引擎的笔记

最近用到thinkphp官方出版的onethink搭建管理后台,因为公司以前代码是smarty写的,模版2000多行,实在懒得用think的模版引擎来改, 所以整合smarty模版引擎, 兼容 think模版. 一:Application/Common/Conf/config.php添加 'TMPL_ENGINE_TYPE'      => 'Smarty', 二:下载smarty模版引擎 下载地址 http://pan.baidu.com/s/1ntoXvwH 放在ThinkPHP\Libra

smarty 模版引擎block疑问

问题描述 smarty 模版引擎block疑问 请问一下,在smarty模版上的这个写法是什么意思?在线等..... 解决方案 SMARTY模版仿smarty的建议模版引擎类PHP 模版引擎Smarty介绍 解决方案二: Smarty末班引擎中提供了三种插件支持,分别是block(块),function(函数),modifier(调节器),用户可以自己扩展. block:是一种非常灵活的高级插件,这种插件在模板中使用时需要成对出现,Smarty内置的block插件例如section,foreac

模版引擎中如何动态引用css

问题描述 body模版页面中写的css和js文件的引用,我要它显示在head里面,而不是<body></body>中.我用过cakePHP,layout页面default.ctp中,写好echo $scripts_for_layout;在view.ctp中写<?php $this->Html->script(array('view'), array('inline' => false )); ?> 就能帮我在heard中加上<script typ

使用TinyButStrong模板引擎来做WEB开发_php模板

使用TinyButStrong模板引擎来做WEB开发 /google 的广告条--> TinyButStrong是应用于4.0.6及以上PHP版本的专业和易用的模板引擎! TinyButStrong 简称TBS,中译为"小强",它是一个PHP类.仅仅包含8个方法3个特性.文件大小100K左右,却能实现超大型模板引擎同样的功能.支持WYSIWYG编写html模板(Dreamweaver .FrontPage等)非常直观方便. 本文将介绍TBS的初步用法.TinyButStrong手

深入解析PHP的Yii框架中的缓存功能_php技巧

数据缓存是指将一些 PHP 变量存储到缓存中,使用时再从缓存中取回.它也是更高级缓存特性的基础,例如查询缓存和内容缓存. 如下代码是一个典型的数据缓存使用模式.其中 $cache 指向缓存组件: // 尝试从缓存中取回 $data $data = $cache->get($key); if ($data === false) { // $data 在缓存中没有找到,则重新计算它的值 // 将 $data 存放到缓存供下次使用 $cache->set($key, $data); } // 这儿

php Smarty初体验二 获取配置信息_php模板

先看结果-- 页面源代码如下: 分析一下代码,经过检查index_config.php(Smarty连接文件).index.php文件均无错,下面重点看看模板文件,可能原因就出在这个tpl文件上,出代码-- 复制代码 代码如下: {% config_load file="1.conf" section="style2" %} <html> <head> <meta http-equiv="Content-Type"

基于thinkphp的onethink如何整合smarty模版

整合smarty模版引擎, 兼容 think模版的步骤如下: 一:Application/Common/Conf/config.php添加 'TMPL_ENGINE_TYPE'      => 'Smarty', 二:下载smarty模版引擎 下载地址 http://www.smarty.net/ 放在ThinkPHP\Library\Vendor下面 因为onethink已经有smarty类,在 ThinkPHP/Library/Think/Template/Driver/Smarty.cla