CodeIgniter整合Smarty的方法詳解
本文實例講述了CodeIgniter整合Smarty的方法。分享給大家供大家參考,具體如下:
CI3.0.2發(fā)布后感覺模板類還是不怎么好用,而且不能編譯。Smarty功能強大,用習慣了Smarty標簽,一般難以放棄,而且,是可以編譯文件執(zhí)行,速度快,我們可以把它們整合使用,彌補CI的模板功能的不足。我們整合使用的是CI版本3.0.3及 Smarty版本3.1.27。下面描述整合過程。
1、下載smarty-3.1.27
2 、解壓smarty-3.1.27到CI項目中的application\libraries下面,其他的文件刪除。
3、 在application\libraries目錄下創(chuàng)建Ci_smarty.php文件,代碼如下:
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require(APPPATH.'libraries/smarty-3.1.27/libs/Smarty.class.php'); class Ci_smarty extends Smarty { protected $ci; public function __construct() { parent::__construct(); $this->ci = & get_instance(); $this->ci->load->config('smarty');//加載smarty的配置文件 $this->cache_lifetime =$this->ci->config->item('cache_lifetime'); $this->caching = $this->ci->config->item('caching'); $this->config_dir = $this->ci->config->item('config_dir'); $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->use_sub_dirs = $this->ci->config->item('use_sub_dirs'); $this->left_delimiter = $this->ci->config->item('left_delimiter'); $this->right_delimiter = $this->ci->config->item('right_delimiter'); } }
4、在application\config目錄下創(chuàng)建配置文件smarty.php,代碼如下:
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $config['cache_lifetime'] = 60; $config['caching'] = false; $config['template_dir'] = APPPATH .'views'; $config['compile_dir'] = APPPATH .'views/template_c'; $config['cache_dir'] = APPPATH . 'views/cache'; $config['config_dir'] = APPPATH . 'views/config'; $config['use_sub_dirs'] = false; //子目錄變量(是否在緩存文件夾中生成子目錄) $config['left_delimiter'] = '{'; $config['right_delimiter'] = '}';
5、在application\core創(chuàng)建MY_controller.php,代碼如下:
class MY_controller extends CI_Controller { public function __construct() { parent::__construct(); } public function assign($key,$val) { $this->ci_smarty->assign($key,$val); } public function display($html) { $this->ci_smarty->display($html); } }
至此,配置整合工作over了,下面我們要驗證是否配置成功。
7、修改application\controllers的Welcome.php,代碼如下:
defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends MY_controller { public function index() { $test='ci 3.0.3 + smarty 3.1.27 配置成功'; $this->assign('test',$test); $this->display('test.html'); } }
然后,在application\views下創(chuàng)建test.html文件,代碼如下:
{$test}
在瀏覽器地址欄中輸入:http://localhost/index.php/Welcome
結(jié)果顯示:
ci 3.0.3 + smarty 3.1.27 配置成功
大功告成!
更多關于CodeIgniter相關內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設計有所幫助。
相關文章
tp5 實現(xiàn)列表數(shù)據(jù)根據(jù)狀態(tài)排序
今天小編就為大家分享一篇tp5 實現(xiàn)列表數(shù)據(jù)根據(jù)狀態(tài)排序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10phpstorm安裝xdebug(phpstudy環(huán)境下)成功運行的操作步驟
這篇文章主要介紹了phpstorm安裝xdebug(phpstudy環(huán)境下)成功運行,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09php如何根據(jù)IP獲取當前經(jīng)緯度以及地域信息
這篇文章主要給大家介紹了關于php如何根據(jù)IP獲取當前經(jīng)緯度以及地域信息的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-01-01thinkPHP5.0框架API優(yōu)化后的友好性分析
這篇文章主要介紹了thinkPHP5.0框架API優(yōu)化后的友好性,結(jié)合具體實例分析了thinkPHP5.0框架進行API優(yōu)化后在數(shù)據(jù)輸出及錯誤調(diào)試方面的友好性改進,需要的朋友可以參考下2017-03-03在Laravel的Model層做數(shù)據(jù)緩存的實現(xiàn)
這篇文章主要介紹了在Laravel的Model層做數(shù)據(jù)緩存的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09thinkphp5.1框架中容器(Container)和門面(Facade)的實現(xiàn)方法分析
這篇文章主要介紹了thinkphp5.1框架中容器(Container)和門面(Facade)的實現(xiàn)方法,結(jié)合實例形式分析了thinkPHP5.1框架中容器與門面的定義、實現(xiàn)方法及相關操作注意事項,需要的朋友可以參考下2019-08-08