用户
 找回密码
 入住 CI 中国社区
搜索
查看: 12100|回复: 19
收起左侧

CI集成Smarty的最佳方式

[复制链接]
发表于 2013-11-2 06:22:02 | 显示全部楼层 |阅读模式
本帖最后由 剑出惊雷 于 2013-11-2 06:38 编辑

RT
因为CI自带的模板功能不是很方便,所以大家普遍采用集成Smarty的方式来弥补CI这方面的不足。
本人在网上看了不少CI集成Smarty的教程,包括咱们CI论坛里面的一个精华帖子
http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=10345

自己对比了一下这些教程,我认为下面这个方案是所有里面最优秀的,强烈推荐给大家(当然也是我自己采取的方案)
出处:
http://www.cnmiss.cn/?p=261
原文里面的一些错误,我在本文里面已经做了修正
-----------------------------------------------------------------------------------------------------------------------------------------------------
下面说下我认为它更加优秀的原因,对比下这个方案和我们论坛的方案,你会发现,这个方案多了一点就是它扩展了核心类
它将Smarty类方法assign和display扩展到Ci的控制器中,所以我们在CI中使用Smarty的时候可以像这样使用:
    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');
    }
通过对核心控制器类的简单扩展,大家在CI中使用Smary的时候和直接使用Smarty的用法习惯是一样的,这是一个很大的优点啊。
而且从核心类库的扩展来看,大家也可以看出该文作者对于CI框架的理解是很好的。
根据这篇文章,我不仅成功集成了Smaty,而且也进一步加强了对于CI的理解。

而且该方案将Smarty的配置文件放到了CI框架的config目录下,对于两者,使用都很规范。
最终实现了"CI和Smaty的无缝结合"。
------------------------------------------------------------------------------------------------------------------------------------------------------
下面开始是具体教程: // 我在原文的基础上做了一些修改,更正了原文的一些错误 注意下文中有'//'的地方,是我自己修改过的地方,或是自己又增加的地方。

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,里面的内容如下:
<?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文件,里面的内容如下:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$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 内容如下: // 扩展核心控制类

<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
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);
    }
}
配置完毕

------------------------------------------------------------------------------------------------------------------------------------------------------
使用方法实例:
在控制器中如:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{$test.title}</title> // 原文是 <title>{$test['title']}</title>,是错误的写法,也有可能是Smarty版本的原因

<style type="text/css">
</style>
</head>
<body>
{$test.num|md5} // 原文这里也写错了
<br>
{$tmp}
</body>
</html>
------------------------------------------------------------------------END----------------------------------------------
大家在实际操作中遇到问题可以向我留言哦
最后 请管理员射精





发表于 2016-1-8 15:01:42 | 显示全部楼层
纠正两个错误:适应smarty3
<?php   
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();
即可。
发表于 2013-12-17 11:25:32 | 显示全部楼层
HTML复制代码
 
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
 
<h4>A PHP Error was encountered</h4>
 
<p>Severity: User Notice</p>
<p>Message:  Undefined property: Cismarty:complie_dir</p>
<p>Filename: libs/Smarty.class.php</p>
<p>Line Number: 700</p>
 
</div>
 
复制代码

 楼主| 发表于 2013-12-21 23:48:37 | 显示全部楼层
<script></script>
发表于 2013-11-2 09:58:42 | 显示全部楼层
真是 smarty 不死啊...  smarty 把模版搞得太复杂了
 楼主| 发表于 2013-11-3 02:40:59 | 显示全部楼层
Hick 发表于 2013-11-2 09:58
真是 smarty 不死啊...  smarty 把模版搞得太复杂了

用Smarty的话,模板的设计、组织和拆分更加灵活多变
而且还额外提供了一些不错的函数

发表于 2013-12-14 10:35:48 | 显示全部楼层
我照着做了,会出现下面的错误

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome:cismarty

Filename: core/MY_Controller.php

Line Number: 9


Fatal error: Call to a member function assign() on a non-object in F:\webroot\ci001\application\core\MY_Controller.php on line 9
发表于 2013-12-14 10:36:16 | 显示全部楼层
HTML复制代码
 
 
A PHP Error was encountered
 
Severity: Notice
 
Message: Undefined property: Welcome:cismarty
 
Filename: core/MY_Controller.php
 
Line Number: 9
 
 
Fatal error: Call to a member function assign() on a non-object in F:\webroot\ci001\application\core\MY_Controller.php on line 9
 
复制代码
发表于 2013-12-14 10:48:27 | 显示全部楼层
照楼主说的,最后访问出现这个,楼主能否指点下
HTML复制代码
 
A PHP Error was encountered
 
Severity: Notice
 
Message: Undefined property: Welcome:cismarty
 
Filename: core/MY_Controller.php
 
Line Number: 9
 
 
Fatal error: Call to a member function assign() on a non-object in F:\webroot\ci001\application\core\MY_Controller.php on line 9
 
 
 
复制代码
发表于 2013-12-14 10:51:18 | 显示全部楼层
正要使用
发表于 2013-12-17 11:24:12 | 显示全部楼层
[img][/img]
发表于 2013-12-17 11:24:55 | 显示全部楼层
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: User Notice</p>
<p>Message:  Undefined property: Cismarty:complie_dir</p>
<p>Filename: libs/Smarty.class.php</p>
<p>Line Number: 700</p>

</div>

本版积分规则