天天看点

smarty2与smarty3的区别

smarty3和smarty2有许多的不同,但是关于smarty3的文档非常少,并且有许多的错误,包括官方的smarty3的手册中因为采用了smarty2的一些范例,也是错误的。

 以下知识点均针对smarty3

----compile_check:模板编辑设置知识点如果将模板编辑关闭($smarty->compile_check = false;),那么修改模板后也无法输出最新的内容。如果想要得到最新的输出,有两种方法:1、将compile_check设置成true(默认)2、同时将对应的编译模板文件(clearCompiledTemplate)和缓存文件(clearCache)删除(如果开启了缓存,如果缓存没开启,则不用)。 在调试的时候如果启用了force_compile,那么就每次都会重新编辑模板(不管其是否更新,直接忽视compile_check限制),并且如果开启了缓存,那么每次都会重新生成缓存。 ----每面多个缓存  ----smarty3和smarty2的区别 扩展设置:请参考smarty3英文pdf文档的扩展设置案例 动作函数名称:格式由类似clear_all_cache()变成clearCache()、英文手册中的例子中的$smarty->cache->clear应该是错误的。 ----缓存打开缓存(默认是关闭的): $smarty->caching  = Smarty::CACHING_LIFETIME_CURRENT;  //开启缓存 $smarty->display("index.tpl","id");  //每页多个缓存页面 也可以在页头全局设置 $smarty->cache_id = "id",isCache和就可以不用设置了。 $smarty->isCache("index.tpl","id");  //判断页面是否缓存,用途:如果缓存已经存在则不读取数据库,反之亦然。一般后台如果有数据更新就会将对应的缓存删除。也可以在页头全局设置  

$smarty->cache_id = "id",isCache和就可以不用设置了。

首页缓存一天

$Smarty->caching=true;$Smarty->cache_lifetime=86400;if(!$Smarty->is_cached('index.html')){ ...... } //文章页 缓存 1周$Smarty->caching=true;$Smarty->cache_lifetime=7*86400;if(!$Smarty->is_cached('article.php',$id)){}

为了防止把执行速度也CACHE了

function nocache_block($params,$content,Smarty $Smarty){reurn$content;}$Smarty->register_block('nocache','nocache_block',false);

调用

<p> Processed in <!--{nocache}--><!--{$runtime}--><!--{/nocache}--></p>

引用另一种防止CACHE的方法

1、使用insert函数使模板的一部分不被缓存

首先在php页面中

<?phpfunction insert_get_now_time(){returndate("Y-m-d h:i:s",time()+3600*8);}     ?>//html调用方法现在时间为:<{insert name="get_now_time"}>

意:首先 函数命名一定要 以 insert_ 开头 后面紧跟着 模版中的函数名字

只要定义了函数 smarty 会自动 加载其函数 。

另一种

2使用register_function阻止插件从缓存中输出

 index.tpl:<div>{current_time}</div> index.php:function smarty_function_current_time($params,&$smarty){returndate("Y-m-d H:m:s");} $smarty=new smarty();$smarty->caching=true;$smarty->register_function('current_time','smarty_function_current_time',false);if(!$smarty->is_cached()){.......}$smarty->display('index.tpl');

注解:

定义一个函数,函数名格式为:smarty_type_name($params,&$smarty)

type为function

name为用户自定义标签名称,在这里是{current_time}

两个参数是必须的,即使在函数中没有使用也要写上。两个参数的功能同上。