如何利用CodeIgniter整合Smarty

不言
發布: 2023-04-01 08:22:01
原創
1508 人瀏覽過

這篇文章主要介紹了CodeIgniter整合Smarty的方法,結合實例形式分析了CodeIgniter3.0.3整合Smarty3.1.27的步驟與相關設定技巧,需要的朋友可以參考下

本文實例講述了CodeIgniter整合Smarty的方法。分享給大家供大家參考,具體如下:

CI3.0.2發布後感覺模板類別還是不怎麼好用,而且不能編譯。 Smarty功能強大,用習慣了Smarty標籤,一般難以放棄,而且,是可以編譯文件執行,速度快,我們可以把它們整合使用,彌補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目錄下建立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目錄下建立設定檔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建立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下建立test.html文件,程式碼如下:

{$test}
登入後複製

在瀏覽器網址列輸入:http://localhost/index.php/Welcome

#結果顯示:

ci 3.0.3 + smarty 3.1.27 配置成功
登入後複製

大功告成!

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

關於在Codeigniter中整合smarty和adodb的方法解析

##關於PHP的Symfony和CodeIgniter框架的Nginx重寫規則配置

#

以上是如何利用CodeIgniter整合Smarty的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!