Hex 发表于 2011-8-26 19:06:01

CodeIngiter 2.0.x 与 Smarty3 整合的方案

作者:风来西林
来源: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的配置方案,你可以根据自己的需要按里面的代码重新配置。下载地址:

第四步:在system目录下创建一个libs目录,然后在libs下创建一个smarty目录。结构如下:
system/libs/smarty
然后在将smarty的类库文件放入到上面的smarty目录下。

第五步:将codeigniter-smarty-3.zip包里的文件直接拷贝到根目录下的对应文件夹中。然后创建Smarty 的类库文件Smarty.php。代码如下:
<?phpif (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Smarty Class
*
* @package    CodeIgniter
* @subpackageLibraries
* @categorySmarty
* @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.
   *
   * @accesspublic
   * @paramstring
   * @paramarray
   * @parambool
   * @returnstring
   */

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的库名如下:
$autoload['libraries'] = array('smarty');
最后呢,你就可以试试看SMARTY库时候正常工作了。在前面的那个压缩包内已经存放有原作者的测试文件。测试路径为:

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

正确的效果页如下截图:

更新说明:大家在配置好Smarty环境后经常会报下面这个错误!

出现这个错误的时候大家不要急,直接打开libs/sysplugins/smarty_internal_data.php文件,然后查找$_variable这个变量的名称。出错的地方多半是写成了$$_variable了。删除前面多的$符号即可!

hem 发表于 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

codetolife 发表于 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

hem 发表于 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是啥.哈哈!!!!!!!

mingpi 发表于 2011-8-27 10:22:35

很好,下载研究研究!

gaoqinlei96 发表于 2011-8-27 16:25:40

呵呵,我也是用这种方式

sangerenba 发表于 2011-9-2 10:13:28

整合了smarty后,ci框架自带的表格不能用了...:'(

jadexo 发表于 2011-9-3 23:04:01

呵呵~!HEX同学很高兴你转载了我的文章~!

Hex 发表于 2011-9-4 21:51:10

jadexo 发表于 2011-9-3 23:04 static/image/common/back.gif
呵呵~!HEX同学很高兴你转载了我的文章~!

呵呵,非常好的文章,感谢你的分享!

jadexo 发表于 2011-9-5 11:31:56

我现在基本不弄ci了,专攻wordpress系统了。呵呵

jianzhong5137 发表于 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

jianzhong5137 发表于 2011-9-7 14:36:58

问题已解决
页: [1] 2 3 4 5 6
查看完整版本: CodeIngiter 2.0.x 与 Smarty3 整合的方案