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

jopen 11年前發布 | 40K 次閱讀 Smarty CodeIgniter Web框架

因為CI自帶的模板功能不是很方便,所以大家普遍采用集成Smarty的方式來彌補CI這方面的不足。
本人在網上看了不少CI集成Smarty的教程,包括咱們CI論壇里面的一個精華帖子
http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=10345

自己對比了一下這些教程,我認為下面這個方案是所有里面最優秀的,強烈推薦給大家(當然也是我自己采取的方案)
出處:
http://www.cnmiss.cn/?p=261
原文里面的一些錯誤,我在本文里面已經做了修正
-----------------------------------------------------------------------------------------------------------------------------------------------------
下面說下我認為它更加優秀的原因,對比下這個方案和我們論壇的方案,你會發現,這個方案多了一點就是它擴展了核心類
它將Smarty類方法assign和display擴展到Ci的控制器中,所以我們在CI中使用Smarty的時候可以像這樣使用:
    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');
    }
通過對核心控制器類的簡單擴展,大家在CI中使用Smary的時候和直接使用Smarty的用法習慣是一樣的,這是一個很大的優點啊。
而且從核心類庫的擴展來看,大家也可以看出該文作者對于CI框架的理解是很好的。
根據這篇文章,我不僅成功集成了Smaty,而且也進一步加強了對于CI的理解。

而且該方案將Smarty的配置文件放到了CI框架的config目錄下,對于兩者,使用都很規范。
最終實現了"CI和Smaty的無縫結合"。
------------------------------------------------------------------------------------------------------------------------------------------------------
下面開始是具體教程: // 我在原文的基礎上做了一些修改,更正了原文的一些錯誤 注意下文中有'//'的地方,是我自己修改過的地方,或是自己又增加的地方。

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>
------------------------------------------------------------------------END----------------------------------------------

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!