PHP 基于Yii框架中使用smarty模板的方法詳解
更新時間:2013年06月13日 14:57:21 作者:
本篇文章是對在Yii框架中使用smarty模板的方法進行了詳細的分析介紹,需要的朋友參考下
第一種方法
按照YII系統(tǒng)的辦法生成視圖覺得有點麻煩,覺得用smarty更省事。嘗試著把smarty模板加進來了。
date_default_timezone_set("PRC");
class PlaceController extends CController {
protected $_smarty;
function __construct(){
parent::__construct('place');//需要一個參數(shù)來調(diào)用父類的構造函數(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ù),對應SMARTY里的每個類名而言,也是先調(diào)用YII的自動加載類的函 數(shù),但是如果不符合YII自動加載的條件的話,就會執(zhí)行SMARTY的自動加載類的函數(shù),然而,SMARTY的類名在自動加載類的時候,確符合了YII自 動加載類的邏輯語句,結果就是YII使用Include語句要包含的類肯定找不到。
解決的辦法是:當SMARTY的類自動加載的時候,跳出在YII定義的自動加載函數(shù),這樣就會執(zhí)行SMARTY的加載函數(shù)。
具體實現(xiàn)是,修改YIIBase類里面的autoload函數(shù),增加如下代碼
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模板了。
public function actionIndex(){
$this->_smarty->assign('test', '測試');
$this->_smarty->display('create.html');
}
第二種方法:
在protected下的extensions文件夾放入smarty模板插件,并建立CSmarty類文件,內(nèi)容如下
<?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ù)組中加入
'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了。
按照YII系統(tǒng)的辦法生成視圖覺得有點麻煩,覺得用smarty更省事。嘗試著把smarty模板加進來了。
復制代碼 代碼如下:
date_default_timezone_set("PRC");
class PlaceController extends CController {
protected $_smarty;
function __construct(){
parent::__construct('place');//需要一個參數(shù)來調(diào)用父類的構造函數(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ù),對應SMARTY里的每個類名而言,也是先調(diào)用YII的自動加載類的函 數(shù),但是如果不符合YII自動加載的條件的話,就會執(zhí)行SMARTY的自動加載類的函數(shù),然而,SMARTY的類名在自動加載類的時候,確符合了YII自 動加載類的邏輯語句,結果就是YII使用Include語句要包含的類肯定找不到。
解決的辦法是:當SMARTY的類自動加載的時候,跳出在YII定義的自動加載函數(shù),這樣就會執(zhí)行SMARTY的加載函數(shù)。
具體實現(xiàn)是,修改YIIBase類里面的autoload函數(shù),增加如下代碼
復制代碼 代碼如下:
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模板了。
復制代碼 代碼如下:
public function actionIndex(){
$this->_smarty->assign('test', '測試');
$this->_smarty->display('create.html');
}
第二種方法:
在protected下的extensions文件夾放入smarty模板插件,并建立CSmarty類文件,內(nèi)容如下
復制代碼 代碼如下:
<?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ù)組中加入
復制代碼 代碼如下:
'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了。
您可能感興趣的文章:
相關文章
PHP中include/require/include_once/require_once使用心得
include() 、require()語句包含并運行指定文件。這兩結構在包含文件上完全一樣,唯一的區(qū)別是對于錯誤的處理。require()語句在遇到包含文件不存在,或是出錯的時候,就停止即行,并報錯。include()則繼續(xù)即行。2016-08-08
php面向對象中static靜態(tài)屬性與方法的內(nèi)存位置分析
這篇文章主要介紹了php面向對象中static靜態(tài)屬性與方法的內(nèi)存位置,通過內(nèi)存位置實例分析了static靜態(tài)屬性的原理與使用技巧,需要的朋友可以參考下2015-02-02
PHP 將圖片按創(chuàng)建時間進行分類存儲的實現(xiàn)代碼
代碼功能:使用PHP巧妙將圖片按創(chuàng)建時間進行分類存儲 圖片文件屬性須取消只讀屬性,否則無法刪除2010-01-01
php?ZipArchive解壓縮實現(xiàn)后臺管理升級問題詳解
php?ZipArchive可以說是php自帶的一個函數(shù)了,他可對對文件進行壓縮與解壓縮處理,但是使用此類之前我們必須在php.ini中把extension=php_zip.dll前面的分號有沒有去掉,然后再重啟Apache這樣才能使用這個類庫2022-12-12

