CI 框架整合 Smarty 模版引擎

joker丶牧羊人發表於2018-10-22
(本地開發環境下進行: CI 3.x + Smarty 3.x)
本地開發環境準備說明

此處不做過多的說明,你可以下載一個本地整合開發環境(如:XAMPP 或者 wampserver)安裝使用;本文件是基於 MAC、 XAMPP 環境進行介紹。

一、搭建CI框架環境
1、下載 CI 3.x 版本

下載地址: codeigniter.org.cn/download

2、解壓、拷貝、初始訪問

解壓檔案,並將其中的資料夾 application、 system、 和檔案 index.php 拷貝到你的web根目錄下,然後就可以使用CI框架了。至於其他的資料夾和檔案,根據你的心情選擇是否要一起拷貝。

copy_floder_file.png

然後訪問你的web進行測試:http://localhost/xxx/ ,如果得到如下畫面,表示CI框架環境搭建成功。

ci_welcome.png

二、整合 Smarty 模版引擎

1、下載 Smarty 3.x 版本

下載地址: github.com/smarty-php/…

2、解壓
3、在 application/third_party/ 資料夾下建立資料夾 smarty-3.1.30 ,並將解壓好的Smarty庫中的libs資料夾複製到 smarty-3.1.30 資料夾中
4、在application/config下建立smarty.php(其中的路徑和定界符均可以自由定義使用),程式碼如下:
<?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/libraries下建立一個Ci_smarty.php(注意:檔名可以隨意儲存,但接下來的步驟會用到此檔案,所以請區分大小寫),程式碼如下:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(APPPATH .'third_party/smarty-3.1.30/libs/SmartyBC.class.php');
class Ci_smarty extends SmartyBC {
    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');
    }
}
複製程式碼
6、在application/core下新建一個MY_Controller.php ,程式碼如下:
<?php
class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library("Ci_smarty");
    }

    public function assign($key, $val) {
        $this->ci_smarty->assign($key, $val);
    }

    public function display($html) {
        $this->ci_smarty->display($html);
    }
}
複製程式碼
7、測試 Smarty 的整合

在 application/controller/ 目錄下建立一個控制器檔案 Test.php, 程式碼示例如下:

<?php
/**
 * Created by PhpStorm.
 * User: joker
 * Date: 2017/11/12
 * Time: 15:15
 */

class Test extends MY_Controller
{
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $data = [];
        $data['describe'] = '測試CI框架整合Smarty模版引擎!!!';
        $this->assign('data', $data);

        $this->display(APPPATH . 'views/templates/test.tpl');
    }
}
複製程式碼

在 application/views/templates/ 目錄下建立一個控制器檔案 test.tpl, 程式碼示例如下:

<!DOCTYPE html>
<html>
<head>
    <title>測試smarty整合</title>
</head>
<body>

<h1><{$data["describe"]}></h1>

</body>
</html>
複製程式碼

在 application/config/routes.php 檔案中新增控制器的訪問路由,則可以進行訪問測試;如果得到如下的介面,則整合成功:

ci_smarty_success.png
至此,CI框架整合Smarty模版引擎完畢。

【如若文件有錯誤,歡迎大家不吝賜教。本文件是集網上各位大神的資源進行整合的,具體資源來源已經忘記了,如果發現有侵權等行為,請聯絡我,我將對應處理,謝謝~~~】

相關文章