CI框架下smarty3的整合步骤(附代码)

不言
发布: 2018-10-08 14:23:24
转载
1834人浏览过

本篇文章给大家带来的内容是关于ci框架下smarty3的整合步骤(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

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方法去掉第二个参数以及相关判断

以上就是CI框架下smarty3的整合步骤(附代码)的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:博客园网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号