ci 框架整合smarty

第一步:安裝CodeIgniter

點擊立即下載最新版本的Zip包>>

https://ellislab.com/codeigniter/user-guide/installation/downloads.html


解壓後,複製文件夾下面的application、system、index.php至項目根目錄中

wKiom1Qb5i-z6WLOAABGL4I3w1Y123.jpg


第二步:安裝Smarty

點擊下載最新的Zip包>>


解壓之後再將下載之後的smarty模板中wKiom1Qb5wiANPtHAABiw4zuQfs185.jpg放到system文件目錄


第三步:創建smarty.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

  */

/*

echo APPPATH."\n";

echo BASEPATH.'libs/Smarty.class.php';die;*/

require_once( BASEPATH.'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();

             if (method_exists( $CI->output, 'set_output' ))

             {

                 $CI->output->set_output( $this->fetch($template) );

             }

             else

             {

                 $CI->output->final_output = $this->fetch($template);

             }

             return;

         }

         else

         {

             return $this->fetch($template);

         }

    }

 }

// END Smarty Class

  

 ?>


第四步:在application下config目錄下的 autoload.php文件修改如下:

$autoload['libraries'] = array('smarty');


第五步:在application/views 下面創建templates_c 和 templates 二個文件夾  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章