PHP开发工具CI框架下配置smarty模版

CI3.0.3完美整合Smarty3.1.27  (2015-10-11 13:56:47)


CI3.0.2已经发布,但是模板类还是不怎么好用,而且不能编译。Smarty目前最新版为3.1.27,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目录下创建cismarty.php文件,代码如下:

(注意:在application/config中打开autoload.php文件,让cismarty文件自动加载。找到这一行:$autoload['libraries'] = array('database','cismarty');

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 配置成功

8、大功告成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值