CI+Smarty的配置及引用步骤

前言:在CI框架下引用Smarty框架,虽然听说CI也有分离前后台的框架,但是本人已经在前台写惯了Smarty,再者考虑到现在PHP流行的框架太多,比如YII、TP、CI等等,为了日后方便项目组的人员扩大,还是统一使用Smarty吧!CI这个框架刚接触,现在准备引用Smarty,记录下步骤。

环境:【Windows7】【PHP5.4.16】【MySQL5.6.12】【Apache2.4.4】【CI2.1.4】【Smarty3.1.12】


一、配置好环境,将CI包解压到运行环境下。


二、在【application/libraries】建一个php文件Smarty.php(类文件,最好开头大写!)

1)将下面的代码复制进去,保存

<?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->set_output = $this->fetch($template);
      return;
    }else{
      return $this->fetch($template);
    }
  }
}
 
// END Smarty Class


2)在你的【application/views】下创建一个【templates_c】文件夹,用来放Smarty框架产生的类似缓存的文件。


3)注意

   ① 里面的【BASEPATH】变量存的是,你CI框架的各种类库路径。在我这个测试版本下,按照第1)步创建目录就可以了;如果你是其他的CI版本,或者自定义的BASEPATH路径,那么你也要相对应地创建目录;总而言之,就是要保证能够让Smarty.php调用到这个类库!

    ② 如果你想让你的前台文件(tpl)直接放在views根目录下,那么要将变量 $this->template_dir 的值从

APPPATH . "views/templates"  改为 -> <span style="font-family: Arial, Helvetica, sans-serif;">APPPATH . "views"。</span>


三、在【system/libs/】下创建文件夹【smarty】,然后把smarty包里面的整个libs文件夹拷贝进入。


四、配置自动加载smarty类库的命令,编辑文件【application/config/autoload.php】,编辑代码如下:

//原本是:
//$autoload['libraries'] = array('database');
//修改为:
$autoload['libraries'] = array('database','smarty');


————到这里就配置完了,接下来看下如何使用!————————————————————————


五、使用方法

1)在【application/controllers/welcome.php】相关的文件中执行以下代码。

public function index(){	
		
		//这个是原本CI调用页面的方式:
<span style="white-space:pre">		</span>//$this->load->view('welcome_message');
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>//这个是CI引用了Smarty后,调用模板的方式:
<span style="white-space:pre">		</span>//发送相关数据到视图层
<span style="white-space:pre">		</span>$this->smarty->assign("data_name","XieBuQing");
<span style="white-space:pre">			</span>
<span style="white-space:pre">		</span>//访问指定视图层
<span style="white-space:pre">		</span>$this->smarty->display("welcome_message.tpl");	
	}


2)在【application/views/welcome_message.tpl】调用变量。

{$data_name}


3)打开浏览器,访问【http://localhost/codeigniter/index.php】。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值