CodeIgniter整合Smarty的方法详解


Posted in PHP onAugust 25, 2017

本文实例讲述了CodeIgniter整合Smarty的方法。分享给大家供大家参考,具体如下:

CI3.0.2发布后感觉模板类还是不怎么好用,而且不能编译。Smarty功能强大,用习惯了Smarty标签,一般难以放弃,而且,是可以编译文件执行,速度快,我们可以把它们整合使用,弥补CI的模板功能的不足。我们整合使用的是CI版本3.0.3及 Smarty版本3.1.27。下面描述整合过程。

1、下载smarty-3.1.27

2 、解压smarty-3.1.27到CI项目中的application\libraries下面,其他的文件删除。

3、 在application\libraries目录下创建Ci_smarty.php文件,代码如下:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH.'libraries/smarty-3.1.27/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->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');
 }
}

4、在application\config目录下创建配置文件smarty.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\core创建MY_controller.php,代码如下:

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);
 }
}

至此,配置整合工作over了,下面我们要验证是否配置成功。

7、修改application\controllers的Welcome.php,代码如下:

defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends MY_controller {
 public function index()
 {
 $test='ci 3.0.3 + smarty 3.1.27 配置成功';
 $this->assign('test',$test);
 $this->display('test.html');
 }
}

然后,在application\views下创建test.html文件,代码如下:

{$test}

在浏览器地址栏中输入:http://localhost/index.php/Welcome

结果显示:

ci 3.0.3 + smarty 3.1.27 配置成功

大功告成!

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

PHP 相关文章推荐
PHP新手上路(十三)
Oct 09 PHP
IIS6+PHP5+MySQL5+Zend Optimizer+phpMyAdmin安装配置图文教程 2009年
Jun 08 PHP
php 模拟POST|GET操作实现代码
Jul 20 PHP
php下连接mssql2005的代码
Jan 17 PHP
PHP Parse Error: syntax error, unexpected $end 错误的解决办法
Jun 05 PHP
无JS,完全php面向过程数据分页实现代码
Aug 27 PHP
19个超实用的PHP代码片段
Mar 14 PHP
Yii框架调试心得--在页面输出执行sql语句
Dec 25 PHP
php支付宝接口用法分析
Jan 04 PHP
PHP实现恶意DDOS攻击避免带宽占用问题方法
May 27 PHP
Yii2 输出xml格式数据的方法
May 03 PHP
php基于闭包实现函数的自调用(递归)实例分析
Nov 11 PHP
PHP观察者模式原理与简单实现方法示例
Aug 25 #PHP
PHP实现的策略模式简单示例
Aug 25 #PHP
php实现简单的权限管理的示例代码
Aug 25 #PHP
thinkphp 抓取网站的内容并且保存到本地的实例详解
Aug 25 #PHP
Laravel中前端js上传图片到七牛云的示例代码
Sep 04 #PHP
使用YII2框架实现微信公众号中表单提交功能
Sep 04 #PHP
PHP实现批量重命名某个文件夹下所有文件的方法
Sep 04 #PHP
You might like
PHP与javascript对多项选择的处理
2006/10/09 PHP
PHP5.3的垃圾回收机制(动态存储分配方案)深入理解
2012/12/10 PHP
解析php利用正则表达式解决采集内容排版的问题
2013/06/20 PHP
javascript针对DOM的应用分析(四)
2012/04/15 Javascript
jQuery拖动图片删除示例
2013/05/10 Javascript
javascript 判断字符串是否包含某字符串及indexOf使用示例
2013/10/18 Javascript
如何从jQuery的ajax请求中删除X-Requested-With
2013/12/11 Javascript
Node.js 服务器端应用开发框架 -- Hapi.js
2014/07/29 Javascript
浅谈时钟的生成(js手写简洁代码)
2016/08/20 Javascript
javascript中异常处理案例(推荐)
2016/10/03 Javascript
JS识别浏览器类型(电脑浏览器和手机浏览器)
2016/11/18 Javascript
js中document.referrer实现移动端返回上一页
2017/02/22 Javascript
浅谈JS对象添加getter与setter的5种方法
2018/06/09 Javascript
Vue-component全局注册实例
2018/09/06 Javascript
Element Card 卡片的具体使用
2020/07/26 Javascript
[46:20]TFT vs Secret Supermajor小组赛C组 BO3 第二场 6.3
2018/06/04 DOTA
[01:51]历届DOTA2国际邀请赛举办地回顾 TI9落地上海
2018/08/26 DOTA
使用IPython下的Net-SNMP来管理类UNIX系统的教程
2015/04/15 Python
python Django批量导入不重复数据
2016/03/25 Python
python使用pyqt写带界面工具的示例代码
2017/10/23 Python
使用Python处理Excel表格的简单方法
2018/06/07 Python
详解python中自定义超时异常的几种方法
2019/07/29 Python
python实现的多任务版udp聊天器功能案例
2019/11/13 Python
在django中自定义字段Field详解
2019/12/03 Python
Python numpy多维数组实现原理详解
2020/03/10 Python
python中sort sorted reverse reversed函数的区别说明
2020/05/11 Python
Tensorflow加载Vgg预训练模型操作
2020/05/26 Python
python爬虫beautifulsoup库使用操作教程全解(python爬虫基础入门)
2021/02/19 Python
高分子材料个人求职信范文
2013/09/25 职场文书
教师应聘个人求职信
2013/12/10 职场文书
青年文明号汇报材料
2014/12/23 职场文书
经验交流材料格式
2014/12/30 职场文书
党员评议自我评价
2015/03/03 职场文书
2016企业先进集体事迹材料
2016/02/25 职场文书
小学四年级作文之最感动的一件事
2019/11/01 职场文书
python3实现常见的排序算法(示例代码)
2021/07/04 Python