1.下載smarty源碼包,解壓放置於項目目錄 libriaries中php
2.在libraries中創建Cismarty.php ,填寫以下代碼html
<?php if(!defined('BASEPATH')) EXIT('No direct script asscess allowed'); require_once( APPPATH . 'libraries/smarty-3.1.27/Smarty.class.php' ); class Cismarty extends Smarty { protected $ci; protected $template_ext; protected $complie_dir; public function __construct(){ parent::__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'); $this->left_delimiter = '<{'; $this->right_delimiter = '}>'; } }
3.在項目目錄的config文件夾內新建文件smarty.php文件,裏面的內容以下:ui
<?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;
4.在入口文件所在目錄新建文件夾templates_c、cache、configs; this
5.在項目目錄下面的config目錄中找到autoload.php文件 spa
$autoload['libraries'] = array('Cismarty');
6.在項目目錄的core文件夾中新建文件MY_Controller.php 內容以下:code
<?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); } }
以上,配置完畢。htm