php ci框架 模板输出,CI框架中使用通用模板引擎smarty
CI版本:2.1.4 // 此時(shí)的最新版本
Smarty版本:Smarty-2.6.26 // 因?yàn)槲抑坝眠@個(gè)版本,為了照顧自己的使用習(xí)慣,這里沒(méi)有使用最新的Smaty版本,大家理解了擴(kuò)展原理,可以選擇自己想用的Smatry版本。
1、到相應(yīng)站點(diǎn)下載Smarty的源碼包; // 我這里用的是 Smarty-2.6.26
2、將源碼包里面的libs文件夾copy到CI的項(xiàng)目目錄下面的libraries文件夾下,并重命名為Smarty-2.6.26;//
3、在項(xiàng)目目錄的libraries文件夾內(nèi)新建文件Cismarty.php,里面的內(nèi)容如下:
if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');
require_once( APPPATH . 'libraries/Smarty-2.6.26/libs/Smarty.class.php' );
class Cismarty extends Smarty {
protected $ci;
public function __construct(){
$this->ci = & get_instance();
$this->ci->load->config('smarty');//加載smarty的配置文件
//獲取相關(guān)的配置項(xiàng)
$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');
}
}
4、在項(xiàng)目目錄的config文件夾內(nèi)新建文件smarty.php文件,里面的內(nèi)容如下:<?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;
5、在入口文件所在目錄新建文件夾templates_c、cache、configs;
6、在項(xiàng)目目錄下面的config目錄中找到autoload.php文件
修改這項(xiàng)
$autoload['libraries'] = array('Cismarty');//目的是:讓系統(tǒng)運(yùn)行時(shí),自動(dòng)加載,不用認(rèn)為的在控制器中手動(dòng)加載
7、在項(xiàng)目目錄的core文件夾中新建文件MY_Controller.php 內(nèi)容如下: // 擴(kuò)展核心控制類<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
class MY_Controller extends CI_Controller { // 原文這里寫錯(cuò)
public function __construct() {
parent::__construct();
}
public function assign($key,$val) {
$this->cismarty->assign($key,$val);
}
public function display($html) {
$this->cismarty->display($html);
}
}
配置完畢
------------------------------------------------------------------------------------------------------------------------------------------------------
使用方法實(shí)例:
在控制器中如:<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller { // 原文這里寫錯(cuò)
public function index()
{
//$this->load->view('welcome_message');
$data['title'] = '標(biāo)題';
$data['num'] = '123456789';
//$this->cismarty->assign('data',$data); // 亦可
$this->assign('data',$data);
$this->assign('tmp','hello');
//$this->cismarty->display('test.html'); // 亦可
$this->display('test.html');
}
}
然后再視圖中:試圖文件夾位于項(xiàng)目目錄的views之下:
新建文件test.html
{ $test.title} // 原文是 {$test['title']},是錯(cuò)誤的寫法,也有可能是Smarty版本的原因{$test.num|md5} // 原文這里也寫錯(cuò)了
{$tmp}
學(xué)編程就來(lái) PHP中文網(wǎng) www.php.cn
總結(jié)
以上是生活随笔為你收集整理的php ci框架 模板输出,CI框架中使用通用模板引擎smarty的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 安阳师范学院有几个校区及校区地址(哪个校
- 下一篇: dress是可数名词吗(dress可数吗