欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP 基于Yii框架中使用smarty模板的方法詳解

 更新時間:2013年06月13日 14:57:21   作者:  
本篇文章是對在Yii框架中使用smarty模板的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
第一種方法
按照YII系統(tǒng)的辦法生成視圖覺得有點(diǎn)麻煩,覺得用smarty更省事。嘗試著把smarty模板加進(jìn)來了。
復(fù)制代碼 代碼如下:

date_default_timezone_set("PRC");
class PlaceController extends CController {
protected $_smarty;
function __construct(){
parent::__construct('place');//需要一個參數(shù)來調(diào)用父類的構(gòu)造函數(shù),該參數(shù)為控制器ID
$path = Yii::getPathOfAlias('application');//獲得protected文件夾的絕對路徑
include (dirname($path).DIRECTORY_SEPARATOR.'Smarty'.DIRECTORY_SEPARATOR.'Smarty.class.php');//smarty所在路徑
$this->_smarty = new Smarty();
$this->_smarty->template_dir = dirname($path).DIRECTORY_SEPARATOR.'template'.DIRECTORY_SEPARATOR;//模板路徑
}

主要一個問題是自動加載類執(zhí)行沖突的問題。
YII注冊了一個自動加載類spl_autoload_register(array('YiiBase','autoload')),SMARTY也注冊了一個自動加載類,spl_autoload_register('smartyAutoload'),YII 注冊在前,這樣在遇到一個類名的時候,先執(zhí)行的是YII的自定義自動加載類的函數(shù),對應(yīng)SMARTY里的每個類名而言,也是先調(diào)用YII的自動加載類的函 數(shù),但是如果不符合YII自動加載的條件的話,就會執(zhí)行SMARTY的自動加載類的函數(shù),然而,SMARTY的類名在自動加載類的時候,確符合了YII自 動加載類的邏輯語句,結(jié)果就是YII使用Include語句要包含的類肯定找不到。
解決的辦法是:當(dāng)SMARTY的類自動加載的時候,跳出在YII定義的自動加載函數(shù),這樣就會執(zhí)行SMARTY的加載函數(shù)。
具體實(shí)現(xiàn)是,修改YIIBase類里面的autoload函數(shù),增加如下代碼
復(fù)制代碼 代碼如下:

public static function autoload($className)
{
// use include so that the error PHP file may appear
if(preg_match('/smarty/i', $className)){      //只要類名包含smarty的,無論大小寫,都返回,這樣就跳出了YII自動加載類而去執(zhí)行                                                                                  SMARTY的自動加載類函數(shù)了
return;
}
             YII自動加載類代碼
}

這樣就可以在每個Action里使用smarty模板了。
復(fù)制代碼 代碼如下:

public function actionIndex(){
$this->_smarty->assign('test', '測試');
$this->_smarty->display('create.html');
}

第二種方法:
在protected下的extensions文件夾放入smarty模板插件,并建立CSmarty類文件,內(nèi)容如下
復(fù)制代碼 代碼如下:

<?php
require_once(Yii::getPathOfAlias('application.extensions.smarty').DIRECTORY_SEPARATOR.'Smarty.class.php'); 
    define('SMARTY_VIEW_DIR', Yii::getPathOfAlias('application.views')); 

    class CSmarty extends Smarty { 
        const DIR_SEP = DIRECTORY_SEPARATOR; 
        function __construct() { 
            parent::__construct(); 

            $this->template_dir = SMARTY_VIEW_DIR; 
            $this->compile_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'template_c'; 
            $this->caching = true; 
            $this->cache_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'cache'; 
            $this->left_delimiter  =  '<!--{'; 
            $this->right_delimiter =  '}-->'; 
            $this->cache_lifetime = 3600; 
        } 
        function init() {} 
    } 
    ?>

然后建立samrty所需的template_c,cache等文件夾。
接下來是配置部分
打開protected/config/main.php在components數(shù)組中加入
復(fù)制代碼 代碼如下:

'smarty'=>array(
    'class'=>'application.extensions.CSmarty',
),

最后在action中直接用Yii::app()->smarty就可以試用smarty了。如果每次在action中使用Yii::app()->smarty比較麻煩的話,可以在components下的Controller中可以加入
復(fù)制代碼 代碼如下:

protected $smarty = '';
protected function init() {
       $this->smarty = Yii::app()->smarty;
 }

然后在action中就直接可以用$this->smarty使用smarty了。

相關(guān)文章

最新評論