参考文档:http://hi.baidu.com/321v_cn/item/04a373737b044814d1dcb310
第一步(准备开源代码):
准备CodeIgniter 2.0.3版本(http://codeigniter.org.cn/downloads)
准备Smarty 3.1.3版本(http://www.smarty.net/download)
第二步(部署目录):
1:在htdocs建立一文件夹名为:demo_ci(名字可随意取)
2:解压ci包.将CI文件根目录(index.php同级目录)全有文件和文件夹拷至于demo_ci根目录.
3:解压smarty包,找到 libs文件夹,将libs拷贝至CI文件的application根目录下的libraries,将libs改名为smarty(名称可随意取,同下).在改名为smarty目录同级新建一个文件为:Ci_smarty.php
4:在demo_ci目录下新建tpl(模板文件夹),tpl_c(编辑的模板文件夹),cache(缓存文件夹),configs(配置文件夹)(不解四个文件夹作用,请百度)
第三步(让ci框架支持smarty模板):
1:找到demo_ci目录下的index.php文件,打开,在任意行添加二行代码(请记住ROOT定义的名称):
if(!defined('ROOT'))
define('ROOT',dirname(__FILE__));
if (!defined('DS'))
define('DS', DIRECTORY_SEPARATOR);//兼容linux,windows. 表示"/"
1:找到application/libraries目录下的Ci_smarty.php
打开Ci_smarty.php文件:添加代码如下:
<?php
require (ROOT.DS.APPPATH."libraries".DS."smarty".DS."Smarty.class.php");//APPPATH是ci系统预定义 // 的:application/ ,ROOT.DS是第三步的1定
义的.
class Ci_smarty extends Smarty{//继承smarty类,Ci_smarty注意如果文件名为大写,类名必须也大写
function __construct(){
parent::__construct();
self::loadsmarty();
}
function loadsmarty(){ //配置smarty
$this->template_dir= ROOT .DS .'tpl'.DS;
$this->compile_dir = ROOT .DS.'tpl_c'.DS;
$this->config_dir = ROOT . DS.'configs'.DS;
$this->cache_dir = ROOT . DS.'cache'.DS;
$this->left_delimiter = '<{';
$this->right_delimiter = '}>';
}
}
?>
第四步(测试ci+smarty...):
1:找到demo_ci/tpl
根目录下新建一模板文件test.html
编写如下代码:
<html>
<head></head>
<body>
<{$test_content}> //注意<{$name}>与smarty定义有关
</body>
</html>
1:找到application/controllers
根目录下新建一测试类 ci_smarty_test.php
编写如下代码:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ci_smarty_test extends CI_Controller{ //控ci写法继承CI_Controller
function __construct(){
parent::__construct();
}
function index(){
$this->load->library("ci_smarty");//ci_smarty首字母可小写.不解请百度
$this->ci_smarty->assign("hello","<h1>this is hello page</h1>"); //smarty设置值
$this->ci_smarty->display("test.html");
}
}
?>
第五步:
打开浏览器输入:
http://127.0.0.1/demo_ci/index.php/ci_smarty_test
如果显示为:this is hello page