這篇文章主要介紹了CI框架集成Smarty的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
出處:
http://www.cnmiss.cn/?p=261
原文里面的一些錯(cuò)誤,我在本文里面已經(jīng)做了修正
下面說下我認(rèn)為它更加優(yōu)秀的原因,對比下這個(gè)方案和我們論壇的方案,你會發(fā)現(xiàn),這個(gè)方案多了一點(diǎn)就是它擴(kuò)展了核心類,
它將Smarty類方法assign和display擴(kuò)展到Ci的控制器中,所以我們在CI中使用Smarty的時(shí)候可以像這樣使用:
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'); }
通過對核心控制器類的簡單擴(kuò)展,大家在CI中使用Smary的時(shí)候和直接使用Smarty的用法習(xí)慣是一樣的,這是一個(gè)很大的優(yōu)點(diǎn)啊。
而且從核心類庫的擴(kuò)展來看,大家也可以看出該文作者對于CI框架的理解是很好的。
根據(jù)這篇文章,我不僅成功集成了Smaty,而且也進(jìn)一步加強(qiáng)了對于CI的理解。
而且該方案將Smarty的配置文件放到了CI框架的config目錄下,對于兩者,使用都很規(guī)范。
最終實(shí)現(xiàn)了"CI和Smaty的無縫結(jié)合"。
下面開始是具體教程: // 我在原文的基礎(chǔ)上做了一些修改,更正了原文的一些錯(cuò)誤 注意下文中有'//'的地方,是我自己修改過的地方,或是自己又增加的地方。
CI版本:2.1.4 // (本文發(fā)布時(shí)使用的版本)
Smarty版本:Smarty-2.6.26 // 因?yàn)槲抑坝眠@個(gè)版本,為了照顧自己的使用習(xí)慣,這里沒有使用新的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)容如下:
<?php 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)加載,不用人為的在控制器中手動(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
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>{ $test.title}</title> //( 原文是 <title>{$test['title']}</title>,是錯(cuò)誤的寫法,也有可能是Smarty版本的原因) <style type="text/css"> </style> </head> <body> {$test.num|md5} // 原文這里也寫錯(cuò)了 <br> {$tmp} </body> </html>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“CI框架集成Smarty的示例分析”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
當(dāng)前題目:CI框架集成Smarty的示例分析-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://jinyejixie.com/article6/dpdeog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站營銷、網(wǎng)站收錄、面包屑導(dǎo)航、網(wǎng)站導(dǎo)航、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容