Codeignter 3 集成smarty模版引擎

阅读 (2680)
Thinkphp 集成了smarty,用起来还行,ci也一样可以集成,自己动手丰衣足食

1、CI版本:3.1 Smarty版本:Smarty-3.1.30 1、到相应站点下载Smarty的源码包;

2、将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为smarty

3、在项目目录的libraries文件夹内新建文件Cismarty.php,里面的内容如下:

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
//加载smarty模板引擎 Release 3.1.30
require(APPPATH . 'libraries/smarty/Smarty.class.php');

/**
 * @Author:      Kevin
 * @DateTime:    2016-09-13 11:44:47
 * @Description: CI集成smarty模版引擎库
 */
class Cismarty extends Smarty
{
    protected $ci;
    public function  __construct(){
        parent::__construct();
        //获取CI全局超级对象
        $ci = &get_instance();
        //加载smarty的配置文件
        $ci->load->config('smarty');

        //获取相关的配置项
        $this->template_dir   = $ci->config->item('template_dir');
        $this->compile_dir    = $ci->config->item('compile_dir');
        $this->cache_dir      = $ci->config->item('cache_dir');
        $this->config_dir     = $ci->config->item('config_dir');
        $this->plugins_dir    = $ci->config->item('plugins_dir');
        $this->caching        = $ci->config->item('caching');
        $this->cache_lifetime = $ci->config->item('lefttime');
    }

}

?>
  

4、在项目目录的config文件夹内新建文件smarty.php文件,里面的内容如下:  

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['template_dir'] = APPPATH . 'views';
$config['compile_dir']  = APPPATH . 'third_party/smarty/templates_c';
$config['cache_dir']    = APPPATH . 'third_party/smarty/cache';
$config['config_dir']   = APPPATH . 'third_party/smarty/configs';
$config['plugins_dir']  = APPPATH . 'third_party/smarty/plugins';
$config['caching']      = false;
$config['lefttime']     = 60;

5、在对应目录新建文件夹templates_c、cache、configs、plugins;   
6、在项目目录下面的config目录中找到autoload.php文件   修改这项  

$autoload['libraries'] = array('cismarty');//目的是:让系统运行时,自动加载,不用认为的在控制器中手动加载   

7、在项目目录的core文件夹中新建文件MY_Controller.php 内容如下: // 扩展核心控制类   

<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
    }

    /**
     * [assign 修改默认cismarty调用方式,在controller中可以直接使用$this->assign]
     * @param  [type] $key [description]
     * @param  [type] $val [description]
     * @return [type]      [description]
     */
    public function assign($key,$val) {
        $this->cismarty->assign($key,$val);
    }

    /**
     * [display  修改默认cismarty调用方式,在controller中可以直接使用$this->display]
     * @param  [type] $html [description]
     * @return [type]       [description]
     */
    public function display($html) {
        $this->cismarty->display($html);
    }
}

 
配置完毕  
------------------------------------------------------------------------------------------------------------------------------------------------------   
使用方法实例:   
在控制器中如:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends MY_Controller { // 原文这里写错
    public function index()
    {
        //$this->load->view('welcome_message');
        $data['title'] = '标题';
        $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');
    }
}

然后再视图中:试图文件夹位于项目目录的views之下:   
新建文件test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{$data.title}</title>

<style type="text/css">
</style>
</head>
<body>
{$data.num|md5}
<br>
{$tmp}
</body>
</html>

 

更新于:2016-09-13 03:52:34
返回顶部