这两天看了不少 ci与smarty 的整合可是最新稳定版整合!特意整理了
http://my.oschina.net/observer/blog/185291
ci 2.1.4 + smarty 3.1.15 配置成功
一、准备文档下载
CodeIgniter_2.1.4点击下载、Smarty-3.1.15 点击下载
二、将Smarty-3.1.15源码包里面的libs文件夹copy到ci的项目目录application下面的libraries文件夹下,并重命名为Smarty-3.1.15
三、application下面的libraries文件夹下,创建文件 Ci_smarty.php
02 | if(!defined('BASEPATH')) exit('No direct script access allowed'); |
03 | require(APPPATH.'libraries/Smarty-3.1.15/Smarty.class.php'); |
04 | class Ci_smarty extends Smarty |
08 | public function __construct() |
10 | parent::__construct(); |
12 | $this->ci = & get_instance(); |
13 | $this->ci->load->config('smarty');//加载smarty的配置文件 |
15 | $this->cache_lifetime = $this->ci->config->item('cache_lifetime'); |
16 | $this->caching = $this->ci->config->item('caching'); |
17 | $this->template_dir = $this->ci->config->item('template_dir'); |
18 | $this->compile_dir = $this->ci->config->item('compile_dir'); |
19 | $this->cache_dir = $this->ci->config->item('cache_dir'); |
20 | $this->use_sub_dirs = $this->ci->config->item('use_sub_dirs'); |
21 | $this->left_delimiter = $this->ci->config->item('left_delimiter'); |
22 | $this->right_delimiter = $this->ci->config->item('right_delimiter'); |
四、在config文件下创建smarty.php,copy 以下
01 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
03 | $config['cache_lifetime'] = 30*24*3600; //更新周期 |
04 | $config['caching'] = false;//是否使用缓存,项目在调试期间,不建议启用缓存 |
05 | $config['template_dir'] = APPPATH.'views'; //设置模板目录 |
06 | $config['compile_dir'] = APPPATH.'views/template_c'; //设置编译目录 |
07 | $config['cache_dir'] = APPPATH.'views/cache';//缓存文件夹 |
08 | $config['use_sub_dirs'] = false; //子目录变量(是否在缓存文件夹中生成子目录) |
09 | $config['left_delimiter'] = '<{'; |
10 | $config['right_delimiter'] = '}>'; |
五、application->config找到autoload.php,修改如下
1 | $autoload['libraries'] = array('Cismarty'); |
六、在application->core下,新建MY_Controller.php,copy 一下
01 | <?php if (!defined('BASEPATH')) exit('No direct access allowed.'); |
02 | class MY_Controller extends CI_Controller { |
03 | public function __construct() { |
04 | parent::__construct(); |
07 | public function assign($key,$val) { |
08 | $this->ci_smarty->assign($key,$val); |
11 | public function display($html) { |
12 | $this->ci_smarty->display($html); |
到此已经配置成功
下面进行测试
在application->controllers目录下新建welcome.php
01 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
02 | class Welcome extends MY_Controller |
04 | public function index() |
06 | $test='ci 2.1.4 + smarty 3.1.15 配置成功'; |
07 | $this->assign('test',$test); |
08 | $this->display('index.html'); |
在application->views目录下新建index.html
04 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
05 | <title>smarty配置测试</title> |