CodeIgniter是一款很优秀的轻量级MVC框架,而Smarty是目前最流行的php模板框架。两者配合起来使用,加快开发效率。
第一步:安装CodeIgniter
解压后,复制文件夹下面的application、system、index.php至项目根目录中
第二步:安装Smarty
在CodeIgniter的application目录下的third_party目录中新建一个名为smarty的目录,将解压出来的libs包复制到该目录中。
第三步:创建模板目录
在application目录的views目录中创建两个文件夹templates、templates_c
第四步:编写安装代码
我是从http://www.coolphptools.com/codeigniter-smarty 下载的代码,但可能版本问题,并不能直接拿来使用,我修改了部分代码。
Smarty.php(复制至appliction/libraries目录中)
'No direct script access allowed');
2:
/**
* Smarty Class
*
* @package CodeIgniter
* @subpackage Libraries
* @category Smarty
* @author Kepler Gelotte
* @link http://www.coolphptools.com/codeigniter-smarty
*/
//require_once( BASEPATH.'libs/smarty/libs/Smarty.class.php' );
'third_party/smarty/libs/Smarty.class.php' );
14:
extends Smarty {
16:
function CI_Smarty()
18: {
parent::Smarty();
20:
;
;
'APPPATH', APPPATH );
'BASEPATH', BASEPATH );
25:
);
27: }
28:
function __construct()
30: {
parent::__construct();
32:
;
;
'APPPATH', APPPATH );
'BASEPATH', BASEPATH );
37:
// Assign CodeIgniter object by reference to CI
'assignByRef') )
40: {
41: $ci =& get_instance();
, $ci);
43: }
44:
);
46: }
47:
48:
/**
* Parse a template using the Smarty engine
*
* This is a convenience method that combines assign() and
* display() into one step.
*
* Values to assign are passed in an associative array of
* name => value pairs.
*
* If the output is to be returned as a string to the caller
* instead of being output, pass true as the third parameter.
*
* @access public
* @param string
* @param array
* @param bool
* @return string
*/
return = FALSE)
68: {
as $key => $val)
70: {
71: $this->assign($key, $val);
72: }
73:
return == FALSE)
75: {
76: $CI =& get_instance();
'set_output' ))
78: {
79: $CI->output->set_output( $this->fetch($template) );
80: }
else
82: {
83: $CI->output->final_output = $this->fetch($template);
84: }
return;
86: }
else
88: {
return $this->fetch($template);
90: }
91: }
92: }
// END Smarty Class