php smarty继承,PHP CI框架继承Smarty步骤及遇到的问题

下面是Smarty2.0的继承方式

------------------------------------------------------------------------------------------------------------------------------------------------------

下面开始是具体教程: // 我在原文的基础上做了一些修改,更正了原文的一些错误 注意下文中有'//'的地方,是我自己修改过的地方,或是自己又增加的地方。

CI版本:2.1.4 // 此时的最新版本

Smarty版本:Smarty-2.6.26 // 因为我之前用这个版本,为了照顾自己的使用习惯,这里没有使用最新的Smaty版本,大家理解了扩展原理,可以选择自己想用的Smatry版本。

1、到相应站点下载Smarty的源码包; // 我这里用的是 Smarty-2.6.26

2、将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty-2.6.26;//

3、在项目目录的libraries文件夹内新建文件Cismarty.php,里面的内容如下:

if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');

require_once( APPPATH . 'libraries/Smarty-2.6.26/libs/Smarty.class.php' );

class Cismarty extends Smarty {

protected $ci;

public function __construct(){

$this->ci = & get_instance();

$this->ci->load->config('smarty');//加载smarty的配置文件

//获取相关的配置项

$this->template_dir = $this->ci->config->item('template_dir');

$this->complie_dir = $this->ci->config->item('compile_dir');

$this->cache_dir = $this->ci->config->item('cache_dir');

$this->config_dir = $this->ci->config->item('config_dir');

$this->template_ext = $this->ci->config->item('template_ext');

$this->caching = $this->ci->config->item('caching');

$this->cache_lifetime = $this->ci->config->item('lefttime');

}

}

4、在项目目录的config文件夹内新建文件smarty.php文件,里面的内容如下:

$config['theme'] = 'default';

$config['template_dir'] = APPPATH . 'views';

$config['compile_dir'] = FCPATH . 'templates_c';

$config['cache_dir'] = FCPATH . 'cache';

$config['config_dir'] = FCPATH . 'configs';

$config['template_ext'] = '.html';

$config['caching'] = false;

$config['lefttime'] = 60;

5、在入口文件所在目录新建文件夹templates_c、cache、configs;

6、在项目目录下面的config目录中找到autoload.php文件

修改这项

$autoload['libraries'] = array('Cismarty');//目的是:让系统运行时,自动加载,不用认为的在控制器中手动加载

7、在项目目录的core文件夹中新建文件MY_Controller.php 内容如下: // 扩展核心控制类

class MY_Controller extends CI_Controller { // 原文这里写错

public function __construct() {

parent::__construct();

}

public function assign($key,$val) {

$this->cismarty->assign($key,$val);

}

public function display($html) {

$this->cismarty->display($html);

}

}

配置完毕

------------------------------------------------------------------------------------------------------------------------------------------------------

使用方法实例:

在控制器中如:

class Welcome extends MY_Controller { // 原文这里写错

public function index()

{

//$this->load->view('welcome_message');

$data['title'] = '标题';

$data['num'] = '123456789';

//$this->cismarty->assign('data',$data); // 亦可

$this->assign('data',$data);

$this->assign('tmp','hello');

//$this->cismarty->display('test.html'); // 亦可

$this->display('test.html');

}

}

然后再视图中:试图文件夹位于项目目录的views之下:

新建文件test.html

{ $test.title} // 原文是 {$test['title']},是错误的写法,也有可能是Smarty版本的原因

{$test.num|md5} // 原文这里也写错了

{$tmp}

------------------------------------------------------------------------start----------------------------------------------

下面是适配Smarty3.0的版本

纠正两个错误:适应smarty3

if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');

require_once( APPPATH . 'libraries/smarty/libs/Smarty.class.php' );

class Cismarty extends Smarty {

protected $ci;

protected $complie_dir;

protected $template_ext;

public function __construct(){

parent::__construct();

$this->ci = & get_instance();

$this->ci->load->config('smarty');//加载smarty的配置文件

//获取相关的配置项

$this->template_dir = $this->ci->config->item('template_dir');

$this->complie_dir = $this->ci->config->item('compile_dir');

$this->cache_dir = $this->ci->config->item('cache_dir');

$this->config_dir = $this->ci->config->item('config_dir');

$this->template_ext = $this->ci->config->item('template_ext');

$this->caching = $this->ci->config->item('caching');

$this->cache_lifetime = $this->ci->config->item('lefttime');

$this->left_delimiter = '{';

$this->right_delimiter = '}';

}

}

在楼主的代码中加入 protected $complie_dir;

protected $template_ext;

parent::__construct();

即可。

------------------------------------------------------------------------end----------------------------------------------

当按照上述方式继承之后,可能会出现下列问题:文件没有写入权限。修改方式:把www/templates_c 目录 改成 777 权限(chmod -R 777 /data/web/app/www/templates_c)

0818b9ca8b590ca3270a3433284dd417.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值