这两天看了不少 ci与smarty 的整合可是最新稳定版整合!特意整理了ci 2.1.4 + smarty 3.1.15 (或 ci 2.2.0 + Smarty-3.1.18等版本都测试通过)配置成功
一、程序下载
下载CodeIgniter2.1.3(以下简称CI)与smarty3.1.12
二、安装CI
解压CI至网站(本次网站测试地址为http://local.ci.com)根目录下面,安装成功后会出现以下内容
三、整合CI与smarty
1、解压smarty压缩包,拷贝libs至ci中application/libraries目录下并改名为smarty
2、在libraries目录下新建smarty.php,添加以下内容
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH.'libraries/smarty/Smarty.class.php');
class CI_Smarty extends Smarty{
public function __construct(){
parent::__construct();
$this->cache_lifetime = 30*24*3600; //更新周期
$this->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存
$this->template_dir = APPPATH.'views'; //设置模板目录
$this->compile_dir = APPPATH.'tpl_c'; //设置编译目录
$this->cache_dir = APPPATH.'cache'; //缓存文件夹
$this->use_sub_dirs = false; //子目录变量(是否在缓存文件夹中生成子目录)
$this->left_delimiter = '<{';// 用于smarty在html中嵌套显示
$this->right_delimiter = '}>';
}
}
注意:__construct()函数里的parent::__construct();一定要加上,不然会出现错误
3、在项目目录下的config文件夹中的autoload.php文件中找到以下代码:
$autoload['libraries'] = array();
// 改为
$autoload['libraries'] = array('smarty');
截止到现在,CI与smarty整合已经完成,接下来我们来进行测试验证配置是否正确
打开application\controllers目录下的welcome.php将所有代码改为
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index(){
$title='smarty配置成功啦!!!';
$this->smarty->assign('title',$title);
$this->smarty->display('index.html');
}
}
在view目录下新建index.html文件,写入
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>smarty配置测试</title>
</head>
<body>
<{$title}>
</body>
</html>
在浏览器地址栏里输入http://local.ci.com,即可看到smarty配置成功啦!!!这条信息