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 相关文章推荐
在PWS上安装PHP4.0正式版
Oct 09 PHP
php下载远程文件类(支持断点续传)
Nov 14 PHP
php mysql 判断update之后是否更新了的方法
Jan 10 PHP
php对二维数组按指定键值key排序示例代码
Nov 26 PHP
php查看请求头信息获取远程图片大小的方法分享
Dec 25 PHP
php $_SERVER windows系统与linux系统下的区别说明
Feb 14 PHP
PHP+MySQL插入操作实例
Jan 21 PHP
php使用GD实现颜色渐变实例
Jun 02 PHP
php创建无限级树型菜单
Nov 05 PHP
PHP序列化操作方法分析
Sep 28 PHP
php删除txt文件指定行及按行读取txt文档数据的方法
Jan 30 PHP
PHP 传输会话curl函数的实例详解
Sep 12 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更改目录及子目录下所有的文件后缀扩展名的代码
2010/10/12 PHP
PHP图片处理类 phpThumb参数用法介绍
2012/03/11 PHP
PHP彩蛋信息介绍和阻止泄漏的方法(隐藏功能)
2014/08/06 PHP
Zend Framework教程之Zend_Config_Ini用法分析
2016/03/23 PHP
PHP+Redis开发的书签案例实战详解
2019/07/09 PHP
PHP查找一列有序数组是否包含某值的方法
2020/02/07 PHP
js使用函数绑定技术改变事件处理程序的作用域
2011/12/26 Javascript
解析jquery获取父窗口的元素
2013/06/26 Javascript
javascript右下角弹层及自动隐藏(自己编写)
2013/11/20 Javascript
javascript中Object使用详解
2015/01/26 Javascript
javascript实现删除前弹出确认框
2015/06/04 Javascript
JavaScript实现网页加载进度条代码超简单
2015/09/21 Javascript
jquery 追加元素append、prepend、before、after用法与区别分析
2016/12/02 Javascript
vue data恢复初始化数据的实现方法
2019/10/31 Javascript
Electron整合React使用搭建开发环境的步骤详解
2020/06/07 Javascript
Python数据结构之Array用法实例
2014/10/09 Python
Python pickle模块用法实例分析
2015/05/27 Python
Python实现购物程序思路及代码
2017/07/24 Python
Python3操作SQL Server数据库(实例讲解)
2017/10/21 Python
机器学习python实战之决策树
2017/11/01 Python
Django框架 信号调度原理解析
2019/09/04 Python
解决TensorFlow模型恢复报错的问题
2020/02/06 Python
Django实现celery定时任务过程解析
2020/04/21 Python
10种CSS3实现的loading动画,挑一个走吧?
2020/11/16 HTML / CSS
全球烹饪课程的领先预订平台:Cookly
2020/01/28 全球购物
全球采购的街头服饰和帽子:Urban Excess
2020/10/28 全球购物
自我评价范文
2013/12/22 职场文书
大学系主任推荐信范文
2013/12/24 职场文书
2014年教师节寄语
2014/08/11 职场文书
总经理检讨书范文
2015/02/16 职场文书
2015年国际护士节演讲稿
2015/03/18 职场文书
数学教师求职信范文
2015/03/20 职场文书
大学军训通讯稿
2015/07/18 职场文书
升学宴来宾致辞
2015/07/27 职场文书
Python趣味挑战之教你用pygame画进度条
2021/05/31 Python
mysql字段为NULL索引是否会失效实例详解
2022/05/30 MySQL