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调用Twitter的RSS的实现代码
Mar 10 PHP
使用bcompiler对PHP文件进行加密的代码
Aug 29 PHP
使用PHP会话(Session)实现用户登陆功能
Jun 29 PHP
PHP图片裁剪函数(保持图像不变形)
May 04 PHP
PHP错误Warning: Cannot modify header information - headers already sent by解决方法
Sep 27 PHP
php实现俄罗斯乘法实例
Mar 07 PHP
深入理解PHP中的Streams工具
Jul 03 PHP
图文详解PHP环境搭建教程
Jul 16 PHP
AES加解密在php接口请求过程中的应用示例
Oct 26 PHP
PHP的消息通信机制测试实例
Nov 10 PHP
tp5(thinkPHP5)框架连接数据库的方法示例
Dec 24 PHP
PHP7内核CGI与FastCGI详解
Apr 14 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和XSS跨站攻击的防范
2007/04/17 PHP
PHP学习笔记之数组篇
2011/06/28 PHP
探讨如何在PHP开启gzip页面压缩实例
2013/06/09 PHP
php面象对象数据库操作类实例
2014/12/02 PHP
php提交post数组参数实例分析
2015/12/17 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
2020/02/27 PHP
在JQuery dialog里的服务器控件 事件失效问题
2010/12/08 Javascript
jquery中获取元素里某一特定子元素的代码
2014/12/02 Javascript
javascript多行字符串的简单实现方式
2015/05/04 Javascript
TypeScript 中接口详解
2015/06/19 Javascript
使用pcs api往免费的百度网盘上传下载文件的方法
2016/03/17 Javascript
jQuery实现下拉框多选 jquery-multiselect 的实例代码
2016/07/14 Javascript
jQuery Mobile和HTML5开发App推广注册页
2016/11/07 Javascript
jquery ui sortable拖拽后保存位置
2017/04/27 jQuery
关于自定义Egg.js的请求级别日志详解
2018/12/12 Javascript
VScode格式化ESlint方法(最全最好用方法)
2019/09/10 Javascript
node.js使用zlib模块进行数据压缩和解压操作示例
2020/02/12 Javascript
[02:49]2014DOTA2电竞也是体育项目! 势要把荣誉带回中国!
2014/07/20 DOTA
Python中模拟enum枚举类型的5种方法分享
2014/11/22 Python
对python的unittest架构公共参数token提取方法详解
2018/12/17 Python
Django forms表单 select下拉框的传值实例
2019/07/19 Python
深入理解css属性的选择对动画性能的影响
2016/04/20 HTML / CSS
通过HTML5规范搞定i、em、b、strong元素的区别
2017/03/04 HTML / CSS
俄罗斯名牌服装网上商店:UNIQUE FABRIC
2019/07/25 全球购物
开学典礼感言
2014/02/16 职场文书
会走路的树教学反思
2014/02/20 职场文书
无房证明范本
2014/09/17 职场文书
2014年法院个人工作总结
2014/12/17 职场文书
学习型家庭事迹材料
2014/12/20 职场文书
邀请函怎么写
2015/01/30 职场文书
毕业晚宴祝酒词
2015/08/11 职场文书
幼儿园开学家长寄语(2016秋季)
2015/12/03 职场文书
日本读研:怎样写好一篇日本研究计划书?
2019/07/15 职场文书
导游词之西安骊山
2019/12/03 职场文书
浅谈Node的内存泄露问题
2022/05/06 NodeJs
apache ftpserver搭建ftp服务器
2022/05/20 Servers