当前位置: 移动技术网 > IT编程>开发语言>PHP > ci框架smarty3整合

ci框架smarty3整合

2018年10月07日  | 移动技术网IT编程  | 我要评论

天然气管道,微信照片发送失败,鼹鼠的故事中文版


1 下载smarty3并将libs文件放在框架libraries目录下重命名为smarty
2 在libraries下创建ci_smarty.php文件,代码如下
<?php  if ( ! defined('basepath')) exit('no direct script access allowed');

require_once(apppath.'libraries/smarty/smarty.class.php'); //这里指定smarty.class.php的存放位置

class ci_smarty extends smarty
{
protected $ci;
public function __construct()
{
parent::__construct();
$this->ci = & get_instance();
$this->ci->load->config('smarty');//加载smarty的配置文件
$this->cache_lifetime =$this->ci->config->item('cache_lifetime');
$this->caching = $this->ci->config->item('caching');
$this->config_dir = $this->ci->config->item('config_dir');
$this->template_dir = $this->ci->config->item('template_dir');
$this->compile_dir = $this->ci->config->item('compile_dir');
$this->cache_dir = $this->ci->config->item('cache_dir');
$this->use_sub_dirs = $this->ci->config->item('use_sub_dirs');
$this->left_delimiter = $this->ci->config->item('left_delimiter');
$this->right_delimiter = $this->ci->config->item('right_delimiter');
}
}

3 在框架config目录下创建smarty.php,代码如下
<?php
$config['cache_lifetime'] = 3600;//缓存失效
$config['caching'] = true;//开启缓存
$config['template_dir'] = apppath .'views';
$config['compile_dir'] = apppath .'views/template_c';
$config['cache_dir'] = apppath . 'views/cache';
$config['config_dir'] = apppath . 'views/config';
$config['use_sub_dirs'] = false; //子目录变量(是否在缓存文件夹中生成子目录)
$config['left_delimiter'] = '{';
$config['right_delimiter'] = '}';
4 在配置文件autoload.php自动加载ci_smarty $autoload['libraries']=array('ci_smarty');
5 在框架的扩展父类my_controller.php(没有就现在core下创建)添加如下代码/ * @param $key * @par * smarty assign */
public function assign($key,$val)
{
$this->cismarty->assign($key,$val);
}

/**
* @param $html
* smarty smarty display方法
*/
public function display($html,$is_cache=false)
{
if(!$is_cache)
{
$this->ci_smarty->clearcache($html);
}
$this->ci_smarty->display($html);
}

/**
* smarty清除所有缓存
* @author shangshikai
*/
public function clearallcache()
{
$this->ci_smarty->clearallcache();
}

/**
* smarty 清除某个模板的缓存
* @author shangshikai
*/
public function clearcache($html)
{
$this->ci_smarty->clearcache($html);
}
/**
* @param $html
* @return mixed
* smarty判断该模板是否有缓存
*/
public function iscached($html)
{
return $this->ci_smarty->iscached($html);
}
6 由于在配置文件smarty.php中开启缓存,但不是所有页面都适合缓存,所以在my_controller中配置display方法时应增加参数默认清除缓存,需要缓存的页面只需在调用display方法时传递第二个参数为true。
在使用缓存后,如果需要局部不需要缓存可以使用{nocache}{/nocache}标签包裹,如果标签不缓存使用方法是在标签后增加nocache 如{foreach $arr as $v nocache}
7 如果整个项目都不使用缓存,可以在smarty.php中去掉$config['cache_lifetime'] = 3600;$config['caching'] = true;两行,并且在my_controller中的display方法去掉第二个
参数以及相关判断

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网