温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php中怎么配置smarty模板引擎存

发布时间:2021-06-30 17:10:44 来源:亿速云 阅读:125 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关php中怎么配置smarty模板引擎存,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

  php中smarty模板引擎存配置方法有什么

  $smarty->cache_dir=“目录名”;//创建缓存目录名

  $smarty->caching=true;//开启缓存,为false的时候缓存无效

  $smarty->cache_lifetime=60;//缓存时间,单位是秒

  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的缓存

  php中smarty模板引擎局部缓存有哪些方法

  第一个:insert_函数默认不缓存,这个属性不能修改

  index.php中,

  functioninsert_get_time(){

  returndate(“Y-m-dH:m:s”);

  }

  index.html中,

  {insertname=“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的内容如下:

  functionsmarty_block_cacheless($param,$content,&$smarty){

  return$content;

  }

  ?>

  2)编写程序及模板

  示例程序:testCacheLess.php

  include('Smarty.class.php');

  $smarty=newSmarty;

  $smarty->caching=true;

  $smarty->cache_lifetime=6;

  $smarty->display('cache.tpl');

  ?>

  所用的模板:cache.tpl

  已经缓存的:{$smarty.now}

  {cacheless}

  没有缓存的:{$smarty.now}

  {/cacheless}

  4自定义缓存

  设置cache_handler_func使用自定义的函数处理缓存

  如:

  $smarty->cache_handler_func=“myCache”;

  functionmyCache($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来压缩和解压

关于php中怎么配置smarty模板引擎存就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI