ci2.2+smarty3.1.20整合

php ci2.2与smarty3.1.20整合

1.下载ci框架与smarty,在此不再赘述

2.将CodeIgniter-2.2-stable.zip解压放到php根目录下

3.解压Smarty-3.1.20.zip,将lib文件夹copy到ci的application\libraries目录下

并新建Ci_smarty.php文件

<?php
/**
*	smarty文件加载类
*
*/
if(!defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH.'libraries/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->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.在ci的application\config目录下

新建smarty.php文件

<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
$config['cache_lifetime']	= 30*24*3600;
$config['caching']	= false;
$config['template_dir']	= APPPATH .'views';
$config['compile_dir']	= APPPATH .'views/template_c';
$config['cache_dir']	= APPPATH . 'views/cache';
$config['use_sub_dirs']	= false;	//子目录变量(是否在缓存文件夹中生成子目录)
$config['left_delimiter']	= '<{';
$config['right_delimiter']	= '}>';
?>

 5.在application/core下新建MY_Controller.php

<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
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);
	}
}
?>

 

6.在application/config/autoload.php文 件添加找到$autoload['libraries']array();添加Ci_smarty如下:

$autoload['libraries'] = array('Ci_smarty');
至此配置完毕,实现了,ci与smarty的无缝整合
7.测试
修改application/controllers/welcome.php如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends MY_Controller 

{

    public function index()

    {

        $test='ci 2.2 + smarty 3.1.20 配置成功';

        $this->assign('test',$test);

        $this->display('test.html');

    }

}
?>
 在applicaction/views下新建test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>smarty配置测试</title>
</head>
<body>
 <{$test}>
</body>
</html>
 

你可能感兴趣的:(smarty)