(1)下載smarty ,這兒下載的是 smarty-3.1.29php
(2)解壓到 application/librarys/文件夾下面,造成 application/librarys/smarty-3.1.29/html
(3)在application/librarys/Cismarty.php新建 Cismarty.php文件app
Cismarty.php 文件修改ui
<?php if(!defined('BASEPATH')) EXIT('No direct script asscess allowed'); require_once( APPPATH . 'libraries/smarty-3.1.29/libs/Smarty.class.php' ); class Cismarty extends Smarty { protected $ci; 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->compile_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->caching = $this->ci->config->item('caching'); $this->cache_lifetime = $this->ci->config->item('lefetime'); $this->right_delimiter= $this->ci->config->item('right_delimiter'); $this->left_delimiter = $this->ci->config->item('left_delimiter'); } }
(4)配置smarty配置項this
<?php if( !defined('BASEPATH')) exit('No direct script access allowed'); $config['theme'] = 'default'; $config['template_dir'] = FCPATH . 'www/temp'; $config['compile_dir'] = FCPATH . 'www/temp_c'; $config['cache_dir'] = FCPATH . 'www/cache'; $config['config_dir'] = FCPATH . 'www/configs'; $config['caching'] = false; $config['lefttime'] = 0; $config['left_delimiter'] = '<{'; $config['right_delimiter'] = '}>';
(5) 在index.php同級目錄下,創建文件夾code
wwwhtm
www/tempip
www/temp_cci
www/cache/get
www/configs
(6)開啓自由加載,application/config/autoload.php
中修改代碼 $autoload['libraries'] = array('Cismarty');
(7)驗證
新建
application/core/MY_Controller.php
<?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); } }
修改application/controller.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends MY_Controller { public function index() { $say = 'hello world'; $this->assign('say', $say); $this->display("index.html"); } }
新建文件www/temp/index.html
<!DOCTYPE> <html> <head> <title></title> </head> <body> <{$say}> </body> </html>
最後 >sudo chmod -R 777 www
訪問localhost/index.php
輸出 hello world
配置完成。