当前位置: 移动技术网 > IT编程>开发语言>PHP > CodeIgniter整合Smarty的方法详解

CodeIgniter整合Smarty的方法详解

2017年12月08日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php优秀开发框架总结》、《thinkphp入门教程》、《thinkphp常用方法总结》、《zend framework框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网