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

CodeIngiter 2.0.x 与 Smarty3 整合的方案

    [复制链接]
发表于 2011-8-26 19:06:01 | 显示全部楼层 |阅读模式
作者:风来西林
来源http://webcaft.com/2011/02/26/132.html


目前CI框架已经推出了2.0的版本了。一直想尝试一下用于我的小说网站的核心。

在国内的网站搜索了一下,基本没有这方面的资料。只要找找E文的网站,功夫不负苦心人,还是被我找到了。经过测试可以正常使用,在一个叫Cool PHP tool的网站。作者提供配置方案和已经配置好的文件,但是西林按他写的步骤总是出错,后来仔细看了一下文件后来发现,那个朋友在打包文件的时候把目录弄错了。

下面我就给大家详细说明一下配置步骤。

第一步:安装CodeIngiter。 这个不需要详细说,下载地址为:http://codeigniter.com/downloads/

第二步:下载最新版本的 Smarty库,下载地址:http://www.smarty.net/download

第三步:下载配置文件包,里面已经写好了Smarty的配置方案,你可以根据自己的需要按里面的代码重新配置。下载地址: codeigniter-smarty-3.zip (12.46 KB, 下载次数: 855)

第四步:在system目录下创建一个libs目录,然后在libs下创建一个smarty目录。结构如下:
system/libs/smarty

然后在将smarty的类库文件放入到上面的smarty目录下。

第五步:将codeigniter-smarty-3.zip包里的文件直接拷贝到根目录下的对应文件夹中。然后创建Smarty 的类库文件Smarty.php。代码如下:
PHP复制代码
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * Smarty Class
 *
 * @package    CodeIgniter
 * @subpackage  Libraries
 * @category  Smarty
 * @author    Kepler Gelotte
 * @link    http://www.coolphptools.com/codeigniter-smarty
 */

 
require_once( BASEPATH.'libs/smarty/libs/Smarty.class.php' );
 
class CI_Smarty extends Smarty {
 
    function CI_Smarty(){
        parent::Smarty();
 
        $this->compile_dir = APPPATH . "views/templates_c";
        $this->template_dir = APPPATH . "views/templates";
        $this->assign( 'APPPATH', APPPATH );
        $this->assign( 'BASEPATH', BASEPATH );
 
        log_message('debug', "Smarty Class Initialized");
    }
 
    function __construct(){
        parent::__construct();
 
        $this->compile_dir = APPPATH . "views/templates_c";
        $this->template_dir = APPPATH . "views/templates";
        $this->assign( 'APPPATH', APPPATH );
        $this->assign( 'BASEPATH', BASEPATH );
 
        // Assign CodeIgniter object by reference to CI
        if ( method_exists( $this, 'assignByRef') ){
            $ci =& get_instance();
            $this->assignByRef("ci", $ci);
        }
        log_message('debug', "Smarty Class Initialized");
    }
 
  /**
   *  Parse a template using the Smarty engine
   * This is a convenience method that combines assign() and
   * display() into one step.
   *
   * Values to assign are passed in an associative array of
   * name => value pairs.
   * If the output is to be returned as a string to the caller
   * instead of being output, pass true as the third parameter.
   *
   * @access  public
   * @param  string
   * @param  array
   * @param  bool
   * @return  string
   */

 
  function view($template, $data = array(), $return = FALSE){
    foreach ($data as $key => $val){
      $this->assign($key, $val);
    }
 
    if ($return == FALSE){
      $CI =& get_instance();
      $CI->output->final_output = $this->fetch($template);
      return;
    }else{
      return $this->fetch($template);
    }
  }
}
 
// END Smarty Class
复制代码

然后再将文件保存到下面的目录下:
application/libraries/Smarty.php

原文中放置的目录是 /system/application/libraries/Smarty.php 但是我这边就报错。

第六步:为让CI每次自动载入Smarty库。打开/system/application/config/autoload.php文件,在$autoload['libraries']中添加smarty的库名如下:
PHP复制代码
$autoload['libraries'] = array('smarty');
复制代码

最后呢,你就可以试试看SMARTY库时候正常工作了。在前面的那个压缩包内已经存放有原作者的测试文件。测试路径为:

http://your-site/index.php/example

正确的效果页如下截图:
test.jpg
更新说明:大家在配置好Smarty环境后经常会报下面这个错误!
error.jpg
出现这个错误的时候大家不要急,直接打开libs/sysplugins/smarty_internal_data.php文件,然后查找$_variable这个变量的名称。出错的地方多半是写成了$$_variable了。删除前面多的$符号即可!

评分

参与人数 1威望 +1 收起 理由
oraclelee + 1 很给力!

查看全部评分

本帖被以下淘专辑推荐:

发表于 2016-3-1 10:44:28 | 显示全部楼层
hem 发表于 2016-3-1 10:43
Fatal error: Cannot access protected property CI_Output:final_output in D:\PHP\www\myWorkSpace\New ...

Fatal error: Cannot access protected property CI_Output:: $final_output in D:\PHP\www\myWorkSpace\News\application\libraries\smarty.php on line 76
发表于 2016-7-8 14:12:49 | 显示全部楼层
@Hex 大神,请问CI3.0导入smarty模板之后会报以下的这个NOTICE,我用的php版本是7 smarty版本用的是3.1.29
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 1

Filename: libs/Smarty.class.php

Line Number: 1181
发表于 2016-3-1 10:43:25 | 显示全部楼层
Fatal error: Cannot access protected property CI_Output:final_output in D:\PHP\www\myWorkSpace\News\application\libraries\smarty.php on line 76

我按照步骤来的。。我smarty是2.X的版本。怎么会报这个错
发表于 2011-8-26 19:34:57 | 显示全部楼层
不错..虽然我不知道smart是啥.哈哈!!!!!!!
发表于 2011-8-27 10:22:35 | 显示全部楼层
很好,下载研究研究!
发表于 2011-8-27 16:25:40 | 显示全部楼层
呵呵,我也是用这种方式
发表于 2011-9-2 10:13:28 | 显示全部楼层
整合了smarty后,ci框架自带的表格不能用了...
发表于 2011-9-3 23:04:01 | 显示全部楼层
呵呵~!HEX同学很高兴你转载了我的文章~!
 楼主| 发表于 2011-9-4 21:51:10 | 显示全部楼层
jadexo 发表于 2011-9-3 23:04
呵呵~!HEX同学很高兴你转载了我的文章~!

呵呵,非常好的文章,感谢你的分享!
发表于 2011-9-5 11:31:56 | 显示全部楼层
我现在基本不弄ci了,专攻wordpress系统了。呵呵
发表于 2011-9-7 14:31:30 | 显示全部楼层
Smarty.php里面
$CI =& get_instance();
$CI->output->final_output = $this->fetch($template);
的final_output为protected的属性,这个应如何改,不改变原CI的基础上
错误:
Fatal error: Cannot access protected property CI_Output:final_output in D:\xampp\htdocs\c202\application\libraries\Smarty.php on line 69
发表于 2011-9-7 14:36:58 | 显示全部楼层
问题已解决

本版积分规则