亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

date_default_timezone_set("PRC");
class PlaceController extends CController {
protected $_smarty;
function __construct(){
parent::__construct('place');//需要一個(gè)參數(shù)來(lái)調(diào)用父類(lèi)的構(gòu)造函數(shù),該參數(shù)為控制器ID
$path = Yii::getPathOfAlias('application');//獲得protected文件夾的絕對(duì)路徑
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;//模板路徑
}

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

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

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

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

第二種方法:
在protected下的extensions文件夾放入smarty模板插件,并建立CSmarty類(lèi)文件,內(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等文件夾。
接下來(lái)是配置部分
打開(kāi)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)文章

最新評(píng)論