PHP的CI框架集成Smarty的最佳方式

来源:互联网 发布:ubuntu镜像下载地址 编辑:程序博客网 时间:2024/05/29 07:31
  1. 因为CI自带的模板功能不是很方便,所以大家普遍采用集成Smarty的方式来弥补CI这方面的不足。  
  2. 本人在网上看了不少CI集成Smarty的教程,包括咱们CI论坛里面的一个精华帖子  
  3. http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=10345。  
  4.   
  5. 自己对比了一下这些教程,我认为下面这个方案是所有里面最优秀的,强烈推荐给大家(当然也是我自己采取的方案)  
  6. 出处:  
  7. http://www.cnmiss.cn/?p=261   
  8. 原文里面的一些错误,我在本文里面已经做了修正  
  9. -----------------------------------------------------------------------------------------------------------------------------------------------------  
  10. 下面说下我认为它更加优秀的原因,对比下这个方案和我们论坛的方案,你会发现,这个方案多了一点就是它扩展了核心类,  
  11. 它将Smarty类方法assign和display扩展到Ci的控制器中,所以我们在CI中使用Smarty的时候可以像这样使用:  
  12.     public function index()  
  13.     {  
  14.         //$this->load->view('welcome_message');  
  15.         $data['title'] = '标题';  
  16.         $data['num'] = '123456789';  
  17.         //$this->cismarty->assign('data',$data); // 也可以  
  18.         $this->assign('data',$data);  
  19.         $this->assign('tmp','hello');  
  20.         //$this->cismarty->display('test.html'); // 也可以  
  21.         $this->display('test.html');  
  22.     }  
  23. 通过对核心控制器类的简单扩展,大家在CI中使用Smary的时候和直接使用Smarty的用法习惯是一样的,这是一个很大的优点啊。  
  24. 而且从核心类库的扩展来看,大家也可以看出该文作者对于CI框架的理解是很好的。  
  25. 根据这篇文章,我不仅成功集成了Smaty,而且也进一步加强了对于CI的理解。  
  26.   
  27. 而且该方案将Smarty的配置文件放到了CI框架的config目录下,对于两者,使用都很规范。  
  28. 最终实现了"CI和Smaty的无缝结合"。  
  29. ------------------------------------------------------------------------------------------------------------------------------------------------------  
  30. 下面开始是具体教程: // 我在原文的基础上做了一些修改,更正了原文的一些错误 注意下文中有'//'的地方,是我自己修改过的地方,或是自己又增加的地方。  
  31.   
  32. CI版本:2.1.4 // 此时的最新版本  
  33. Smarty版本:Smarty-2.6.26 // 因为我之前用这个版本,为了照顾自己的使用习惯,这里没有使用最新的Smaty版本,大家理解了扩展原理,可以选择自己想用的Smatry版本。  
  34.   
  35.   
  36. 1、到相应站点下载Smarty的源码包; // 我这里用的是 Smarty-2.6.26  
  37. 2、将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty-2.6.26;//   

  1. 3、在项目目录的libraries文件夹内新建文件Cismarty.php,里面的内容如下:  
  1. <?php   
  2. if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');   
  3. require_once( APPPATH . 'libraries/Smarty-2.6.26/libs/Smarty.class.php' );   
  4.   
  5. class Cismarty extends Smarty {   
  6.     protected $ci;   
  7.     public function  __construct(){   
  8.         $this->ci = & get_instance();   
  9.         $this->ci->load->config('smarty');//加载smarty的配置文件   
  10.         //获取相关的配置项   
  11.         $this->template_dir   = $this->ci->config->item('template_dir');   
  12.         $this->complie_dir    = $this->ci->config->item('compile_dir');   
  13.         $this->cache_dir      = $this->ci->config->item('cache_dir');   
  14.         $this->config_dir     = $this->ci->config->item('config_dir');   
  15.         $this->template_ext   = $this->ci->config->item('template_ext');   
  16.         $this->caching        = $this->ci->config->item('caching');   
  17.         $this->cache_lifetime = $this->ci->config->item('lefttime');   
  18.     }   
  19. }   
  20.   
  21. 4、在项目目录的config文件夹内新建文件smarty.php文件,里面的内容如下:   
  22.   
  23. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');   
  24. $config['theme']        = 'default';   
  25. $config['template_dir'] = APPPATH . 'views';   
  26. $config['compile_dir']  = FCPATH . 'templates_c';   
  27. $config['cache_dir']    = FCPATH . 'cache';   
  28. $config['config_dir']   = FCPATH . 'configs';   
  29. $config['template_ext'] = '.html';   
  30. $config['caching']      = false;   
  31. $config['lefttime']     = 60;   
  32.   
  33. 5、在入口文件所在目录新建文件夹templates_c、cache、configs;   
  34. 6、在项目目录下面的config目录中找到autoload.php文件   
  35. 修改这项   
  36.   
  37. $autoload['libraries'] = array('Cismarty');//目的是:让系统运行时,自动加载,不用认为的在控制器中手动加载   
  1. 7、在项目目录的core文件夹中新建文件MY_Controller.php 内容如下: // 扩展核心控制类   
  2.   
  3.   
  4. <?php if (!defined('BASEPATH')) exit('No direct access allowed.');   
  5. class MY_Controller extends CI_Controller { // 原文这里写错   
  6.     public function __construct() {   
  7.         parent::__construct();   
  8.     }   
  9.   
  10.     public function assign($key,$val) {   
  11.         $this->cismarty->assign($key,$val);   
  12.     }   
  13.   
  14.     public function display($html) {   
  15.         $this->cismarty->display($html);   
  16.     }   
  17. }   
  18. 配置完毕   
  19.   
  20.   
  21. ------------------------------------------------------------------------------------------------------------------------------------------------------   
  22. 使用方法实例:   
  23. 在控制器中如:   
  24.   
  25. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');   
  26.   
  27. class Welcome extends MY_Controller { // 原文这里写错   
  28.     public function index()   
  29.     {   
  30.         //$this->load->view('welcome_message');   
  31.         $data['title'] = '标题';   
  32.         $data['num'] = '123456789';   
  33.         //$this->cismarty->assign('data',$data); // 亦可   
  34.         $this->assign('data',$data);   
  35.         $this->assign('tmp','hello');   
  36.         //$this->cismarty->display('test.html'); // 亦可   
  37.         $this->display('test.html');   
  38.     }   
  39. }   
  40. 然后再视图中:试图文件夹位于项目目录的views之下:   
  41. 新建文件test.html   

  42. <!DOCTYPE html>   
  1. <html>   
  2. <head>   
  3. <meta charset="utf-8">   
  4. <title>{ $test.title}</title> // 原文是 <title>{$test['title']}</title>,是错误的写法,也有可能是Smarty版本的原因   
  5.   
  6. <style type="text/css">   
  7. </style>   
  8. </head>   
  9. <body>   
  10. {$test.num|md5} // 原文这里也写错了   
  11. <br>   
  12. {$tmp}   
  13. </body>   
  14. </html>   

0 0