Codeigniter整合smarty

smarty的模板機制很強大,一般情況下CI無需整合其他模板標籤,因爲PHP本身就是一種標籤,簡單易用。codeigniter整合smarty教程(我用的都是最新版本)如下:
第一步:下載codeigniter最新版本:http://codeigniter.org.cn/downloads
第二步:下載smarty最新版本:http://www.smarty.net/download
第三步:
配置步驟:
(1)將smarty拷貝到application/libraries下,然後再根目錄下下新建templates,templates_c,config,cache目錄,結構如下:


codeigniter輕鬆整合smarty



(2)入口文件新增:define('ROOT', dirname(__FILE__));
(3)libraries下新建CI_Smarty.php

  1. <?php defined('BASEPATH') or die('Access restricted!');
  2.  
  3. require(APPPATH . 'libraries/smarty/Smarty.class.php');
  4.  
  5. class CI_Smarty extends Smarty {
  6.  
  7. public function __construct($template_dir = '', $compile_dir = '', $config_dir = '', $cache_dir = '') {
  8. parent::__construct();
  9. if (is_array($template_dir)) {
  10. foreach ($template_dir as $key => $value) {
  11. $this->$key = $value;
  12. }
  13. } else {
  14. //ROOT是Codeigniter在入口文件index.php定義的本web應用的根目錄
  15. $this->template_dir = $template_dir ? $template_dir : ROOT . '/templates';
  16. $this->compile_dir = $compile_dir ? $compile_dir : ROOT . '/templates_c';
  17. $this->config_dir = $config_dir ? $config_dir : ROOT . '/config';
  18. $this->cache_dir = $cache_dir ? $cache_dir : ROOT . '/cache';
  19. }
  20. }
  21.  
  22. }

controller中使用:
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. // by www.phpddt.com
  3. class Welcome extends CI_Controller {
  4.  
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->load->library('CI_Smarty');
  9. }
  10. public function test()
  11. {
  12. $this->ci_smarty->assign('test', 'smarty');
  13. $this->ci_smarty->display('test.tpl');
  14. }
  15. }
  16.  
  17. /* End of file welcome.php */
  18. /* Location: ./application/controllers/welcome.php */

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章