PHP CI框架繼承Smarty步驟及遇到的問題

下面是Smarty2.0的繼承方式
------------------------------------------------------------------------------------------------------------------------------------------------------
下面開始是具體教程: // 我在原文的基礎上做了一些修改,更正了原文的一些錯誤 注意下文中有'//'的地方,是我自己修改過的地方,或是自己又增加的地方。

CI版本:2.1.4 // 此時的最新版本
Smarty版本:Smarty-2.6.26 // 因爲我之前用這個版本,爲了照顧自己的使用習慣,這裏沒有使用最新的Smaty版本,大家理解了擴展原理,可以選擇自己想用的Smatry版本。


1、到相應站點下載Smarty的源碼包; // 我這裏用的是 Smarty-2.6.26
2、將源碼包裏面的libs文件夾copy到CI的項目目錄下面的libraries文件夾下,並重命名爲Smarty-2.6.26;// 
3、在項目目錄的libraries文件夾內新建文件Cismarty.php,裏面的內容如下:

<?php 
if(!defined('BASEPATH')) EXIT('No direct script asscess allowed'); 
require_once( APPPATH . 'libraries/Smarty-2.6.26/libs/Smarty.class.php' ); 

class Cismarty extends Smarty { 
    protected $ci; 
    public function  __construct(){ 
        $this->ci = & get_instance(); 
        $this->ci->load->config('smarty');//加載smarty的配置文件 
        //獲取相關的配置項 
        $this->template_dir   = $this->ci->config->item('template_dir'); 
        $this->complie_dir    = $this->ci->config->item('compile_dir'); 
        $this->cache_dir      = $this->ci->config->item('cache_dir'); 
        $this->config_dir     = $this->ci->config->item('config_dir'); 
        $this->template_ext   = $this->ci->config->item('template_ext'); 
        $this->caching        = $this->ci->config->item('caching'); 
        $this->cache_lifetime = $this->ci->config->item('lefttime'); 
    } 
} 

4、在項目目錄的config文件夾內新建文件smarty.php文件,裏面的內容如下: 

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
$config['theme']        = 'default'; 
$config['template_dir'] = APPPATH . 'views'; 
$config['compile_dir']  = FCPATH . 'templates_c'; 
$config['cache_dir']    = FCPATH . 'cache'; 
$config['config_dir']   = FCPATH . 'configs'; 
$config['template_ext'] = '.html'; 
$config['caching']      = false; 
$config['lefttime']     = 60; 

5、在入口文件所在目錄新建文件夾templates_c、cache、configs; 
6、在項目目錄下面的config目錄中找到autoload.php文件 
修改這項 

$autoload['libraries'] = array('Cismarty');//目的是:讓系統運行時,自動加載,不用認爲的在控制器中手動加載 



7、在項目目錄的core文件夾中新建文件MY_Controller.php 內容如下: // 擴展核心控制類 


<?php if (!defined('BASEPATH')) exit('No direct access allowed.'); 
class MY_Controller extends CI_Controller { // 原文這裏寫錯 
    public function __construct() { 
        parent::__construct(); 
    } 

    public function assign($key,$val) { 
        $this->cismarty->assign($key,$val); 
    } 

    public function display($html) { 
        $this->cismarty->display($html); 
    } 
} 
配置完畢 


------------------------------------------------------------------------------------------------------------------------------------------------------ 
使用方法實例: 
在控制器中如: 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Welcome extends MY_Controller { // 原文這裏寫錯 
    public function index() 
    { 
        //$this->load->view('welcome_message'); 
        $data['title'] = '標題'; 
        $data['num'] = '123456789'; 
        //$this->cismarty->assign('data',$data); // 亦可 
        $this->assign('data',$data); 
        $this->assign('tmp','hello'); 
        //$this->cismarty->display('test.html'); // 亦可 
        $this->display('test.html'); 
    } 
} 
然後再視圖中:試圖文件夾位於項目目錄的views之下: 
新建文件test.html 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>{ $test.title}</title> // 原文是 <title>{$test['title']}</title>,是錯誤的寫法,也有可能是Smarty版本的原因 

<style type="text/css"> 
</style> 
</head> 
<body> 
{$test.num|md5} // 原文這裏也寫錯了 
<br> 
{$tmp} 
</body> 
</html> 
<pre name="code" class="php">------------------------------------------------------------------------start----------------------------------------------
下面是適配Smarty3.0的版本

糾正兩個錯誤:適應smarty3
<?php   
if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');   
require_once( APPPATH . 'libraries/smarty/libs/Smarty.class.php' );   
  
class Cismarty extends Smarty {   
    protected $ci; 
    protected $complie_dir;
    protected $template_ext;  
    public function  __construct(){ 
        parent::__construct();  
        $this->ci = & get_instance();   
        $this->ci->load->config('smarty');//加載smarty的配置文件   
        //獲取相關的配置項   
        $this->template_dir   = $this->ci->config->item('template_dir');   
        $this->complie_dir    = $this->ci->config->item('compile_dir');  
        $this->cache_dir      = $this->ci->config->item('cache_dir');   
        $this->config_dir     = $this->ci->config->item('config_dir');   
        $this->template_ext   = $this->ci->config->item('template_ext');   
        $this->caching        = $this->ci->config->item('caching');   
        $this->cache_lifetime = $this->ci->config->item('lefttime'); 

       
        $this->left_delimiter = '{';
        $this->right_delimiter = '}';
         
    }   
} 

在樓主的代碼中加入 protected $complie_dir;
    <span style="white-space:pre">		</span>protected $template_ext;  
<span style="white-space:pre">		</span>parent::__construct(); 
即可。
------------------------------------------------------------------------end----------------------------------------------


當按照上述方式繼承之後,可能會出現下列問題:文件沒有寫入權限。修改方式:把www/templates_c 目錄 改成 777 權限(chmod -R 777 /data/web/app/www/templates_c)


發佈了164 篇原創文章 · 獲贊 1 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章