zoukankan      html  css  js  c++  java
  • CodeIngiter 2.2.x 与 Smarty3 整合的方案 解决CI的不足


    目前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, 下载次数: 591)

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

    【此步骤经过测试直接放在项目目录里面的也是可以的 比如:application/libraries/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

    正确的效果页如下截图:

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

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

  • 相关阅读:
    cf C. Vasya and Robot
    zoj 3805 Machine
    cf B. Vasya and Public Transport
    cf D. Queue
    cf C. Find Maximum
    cf B. Two Heaps
    cf C. Jeff and Rounding
    cf B. Jeff and Periods
    cf A. Jeff and Digits
    I Think I Need a Houseboat
  • 原文地址:https://www.cnblogs.com/dashafa/p/4005976.html
Copyright © 2011-2022 走看看