PHP 基於Yii架構中使用smarty模板的方法詳解_PHP教程

來源:互聯網
上載者:User
第一種方法
按照YII系統的辦法產生視圖覺得有點麻煩,覺得用smarty更省事。嘗試著把smarty模板加進來了。
複製代碼 代碼如下:
date_default_timezone_set("PRC");
class PlaceController extends CController {
protected $_smarty;
function __construct(){
parent::__construct('place');//需要一個參數來調用父類的建構函式,該參數為控制器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;//模板路徑
}

主要一個問題是 自動載入類執行衝突的問題
YII註冊了一個自動載入類spl_autoload_register(array('YiiBase','autoload')),SMARTY也註冊了一個自動載入類,spl_autoload_register('smartyAutoload'),YII 註冊在前,這樣在遇到一個類名的時候,先執行的是YII的自訂自動載入類的函數,對應SMARTY裡的每個類名而言,也是先調用YII的自動載入類的函 數,但是如果不符合YII自動載入的條件的話,就會執行SMARTY的自動載入類的函數,然而,SMARTY的類名在自動載入類的時候,確符合了YII自 動載入類的邏輯語句,結果就是YII使用Include語句要包含的類肯定找不到。
解決的辦法是:當SMARTY的類自動載入的時候,跳出在YII定義的自動載入函數,這樣就會執行SMARTY的載入函數。
具體實現是,修改YIIBase類裡面的autoload函數,增加如下代碼
複製代碼 代碼如下:
public static function autoload($className)
{
// use include so that the error PHP file may appear
if(preg_match('/smarty/i', $className)){ //只要類名包含smarty的,無論大小寫,都返回,這樣就跳出了YII自動載入類而去執行 SMARTY的自動載入類函數了
return;
}
YII自動載入類代碼
}

這樣就可以在每個Action裡使用smarty模板了。
複製代碼 代碼如下:
public function actionIndex(){
$this->_smarty->assign('test', '測試');
$this->_smarty->display('create.html');
}

第二種方法:
在protected下的extensions檔案夾放入smarty模板外掛程式,並建立CSmarty類檔案,內容如下
複製代碼 代碼如下:
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->cache_lifetime = 3600;
}
function init() {}
}
?>

然後建立samrty所需的template_c,cache等檔案夾。
接下來是配置部分
開啟protected/config/main.php在components數組中加入
複製代碼 代碼如下:
'smarty'=>array(
'class'=>'application.extensions.CSmarty',
),

最後在action中直接用Yii::app()->smarty就可以試用smarty了。如果每次在action中使用Yii::app()->smarty比較麻煩的話,可以在components下的Controller中可以加入
複製代碼 代碼如下:
protected $smarty = '';
protected function init() {
$this->smarty = Yii::app()->smarty;
}

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

http://www.bkjia.com/PHPjc/327564.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327564.htmlTechArticle第一種方法 按照YII系統的辦法產生視圖覺得有點麻煩,覺得用smarty更省事。嘗試著把smarty模板加進來了。 複製代碼 代碼如下: date_default_...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.