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>
——————————————————————————————————————————————————————————————
在使用新版的smarty的時候會報錯
Fatal error: Call to a member function createTemplate() on a non-object in D:\ci\application\libraries\smarty\sysplugins\smarty_internal_templatebase.php on line 47
解決後再寫上 先用2.2.26的版本湊合用下