当前位置:Gxlcms > PHP教程 > CI框架下smarty3的整合步骤(附代码)

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

时间:2021-07-01 10:21:17 帮助过:26人阅读

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

1 下载smarty3并将libs文件放在框架libraries目录下重命名为smarty
2 在libraries下创建Ci_smarty.php文件,代码如下

  1. <?php
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. require_once(APPPATH.'libraries/smarty/Smarty.class.php'); //这里指定Smarty.class.php的存放位置
  4. class Ci_smarty extends Smarty
  5. {
  6. protected $ci;
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $this->ci = & get_instance();
  11. $this->ci->load->config('smarty');//加载smarty的配置文件
  12. $this->cache_lifetime =$this->ci->config->item('cache_lifetime');
  13. $this->caching = $this->ci->config->item('caching');
  14. $this->config_dir = $this->ci->config->item('config_dir');
  15. $this->template_dir = $this->ci->config->item('template_dir');
  16. $this->compile_dir = $this->ci->config->item('compile_dir');
  17. $this->cache_dir = $this->ci->config->item('cache_dir');
  18. $this->use_sub_dirs = $this->ci->config->item('use_sub_dirs');
  19. $this->left_delimiter = $this->ci->config->item('left_delimiter');
  20. $this->right_delimiter = $this->ci->config->item('right_delimiter');
  21. }
  22. }

3 在框架config目录下创建smarty.php,代码如下

  1. <?php
  2. $config['cache_lifetime'] = 3600;//缓存失效
  3. $config['caching'] = true;//开启缓存
  4. $config['template_dir'] = APPPATH .'views';
  5. $config['compile_dir'] = APPPATH .'views/template_c';
  6. $config['cache_dir'] = APPPATH . 'views/cache';
  7. $config['config_dir'] = APPPATH . 'views/config';
  8. $config['use_sub_dirs'] = false; //子目录变量(是否在缓存文件夹中生成子目录)
  9. $config['left_delimiter'] = '{';
  10. $config['right_delimiter'] = '}';

4 在配置文件autoload.php自动加载ci_smarty

  1. $autoload['libraries']=array('ci_smarty');

5 在框架的扩展父类MY_Controller.php(没有就现在core下创建)添加如下代码

  1. / * @param $key * @par * smarty assign */
  2. public function assign($key,$val){
  3. $this->cismarty->assign($key,$val);
  4. }
  5. /**
  6. * @param $html
  7. * smarty smarty display方法
  8. */
  9. public function display($html,$is_cache=false){
  10. if(!$is_cache)
  11. {
  12. $this->ci_smarty->clearCache($html);
  13. }
  14. $this->ci_smarty->display($html);}
  15. /**
  16. * smarty清除所有缓存
  17. * @author shangshikai
  18. */
  19. public function clearAllCache(){
  20. $this->ci_smarty->clearAllCache();
  21. }
  22. /**
  23. * smarty 清除某个模板的缓存
  24. * @author shangshikai
  25. */
  26. public function clearCache($html){
  27. $this->ci_smarty->clearCache($html);
  28. }
  1. /**
  2. * @param $html
  3. * @return mixed
  4. * smarty判断该模板是否有缓存
  5. */
  6. public function isCached($html)
  7. {
  8. return $this->ci_smarty->isCached($html);
  9. }

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的整合步骤(附代码)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行