第一步:下載codeigniter最新版本:http://codeigniter.org.cn/downloads
第二步:下載smarty最新版本:http://www.smarty.net/download
第三步:
配置步驟:
(1)將smarty拷貝到application/libraries下,然後再根目錄下下新建templates,templates_c,config,cache目錄,結構如下:
(2)入口文件新增:define('ROOT', dirname(__FILE__));
(3)libraries下新建CI_Smarty.php
- <?php defined('BASEPATH') or die('Access restricted!');
- require(APPPATH . 'libraries/smarty/Smarty.class.php');
- class CI_Smarty extends Smarty {
- public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '') {
- parent::__construct();
- if (is_array($template_dir)) {
- foreach ($template_dir as $key => $value) {
- $this->$key = $value;
- }
- } else {
- //ROOT是Codeigniter在入口文件index.php定義的本web應用的根目錄
- $this->template_dir = $template_dir ? $template_dir : ROOT . '/templates';
- $this->compile_dir = $compile_dir ? $compile_dir : ROOT . '/templates_c';
- $this->config_dir = $config_dir ? $config_dir : ROOT . '/config';
- $this->cache_dir = $cache_dir ? $cache_dir : ROOT . '/cache';
- }
- }
- }
controller中使用:
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- // by www.phpddt.com
- class Welcome extends CI_Controller {
- public function __construct()
- {
- parent::__construct();
- $this->load->library('CI_Smarty');
- }
- public function test()
- {
- $this->ci_smarty->assign('test', 'smarty');
- $this->ci_smarty->display('test.tpl');
- }
- }
- /* End of file welcome.php */
- /* Location: ./application/controllers/welcome.php */