Yii中render和renderPartial的區(qū)別
以下由我們?cè)谛乓拙W(wǎng)絡(luò)公司開發(fā)項(xiàng)目的時(shí)候終結(jié)出的一些經(jīng)驗(yàn)
在進(jìn)行頁面輸出渲染的時(shí)候。
1.render 輸出父模板的內(nèi)容,將渲染的內(nèi)容,嵌入父模板。|
2.renderPartial 則不輸出父模板的內(nèi)容。只對(duì)本次渲染的局部?jī)?nèi)容,進(jìn)行輸出。
同時(shí)還有個(gè)重要的區(qū)別:
render 函數(shù)內(nèi)部默認(rèn)執(zhí)行processOutput($output)函數(shù), 會(huì)將把組件,比如 CTreeView 里面注冊(cè)到 CClientScript 里面的
需要的腳本進(jìn)行渲染輸出。
而renderPartial() 默認(rèn)不自動(dòng)渲染輸出客戶端腳本,需要進(jìn)行參數(shù)的指定,才會(huì)輸出:
renderPartial($view,$data=null,$return=false,$processOutput=false)
指定processOutput 為 true 即可。
比如要局部輸出 CTreeView ,用renderPartial 進(jìn)行渲染,如果按照默認(rèn)processOutput=false 則輸出內(nèi)容,不含有客戶端腳本
輸出內(nèi)容則為 正常的 ul 列表。沒有樹形的折疊效果。 主動(dòng)設(shè)定 processOutput=true 后,CTreeView 所需的,所有客戶端腳本就會(huì)被正常輸出在列表的前面。
下面介紹下要用到的幾個(gè)相關(guān)的函數(shù):
render,renderPartial 不再介紹
processOutput()
<?php
publicfunction render($view,$data=null,$return=false)
{
if($this->beforeRender($view))
{
$output=$this->renderPartial($view,$data,true);
if(($layoutFile=$this->getLayoutFile($this->layout))!==false)
$output=$this->renderFile($layoutFile,array('content'=>$output),true);
$this->afterRender($view,$output);
$output=$this->processOutput($output);
if($return)
return $output;
else
echo $output;
}
}
publicfunction renderPartial($view,$data=null,$return=false,$processOutput=false)
{
if(($viewFile=$this->getViewFile($view))!==false)
{
$output=$this->renderFile($viewFile,$data,true);
if($processOutput)
$output=$this->processOutput($output);
if($return)
return $output;
else
echo $output;
}
else
thrownewCException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
array('{controller}'=>get_class($this),'{view}'=>$view)));
}
publicfunction processOutput($output)
{
Yii::app()->getClientScript()->render($output);
// if using page caching, we should delay dynamic output replacement
if($this->_dynamicOutput!==null&& $this->isCachingStackEmpty())
{
$output=$this->processDynamicOutput($output);
$this->_dynamicOutput=null;
}
if($this->_pageStates===null)
$this->_pageStates=$this->loadPageStates();
if(!empty($this->_pageStates))
$this->savePageStates($this->_pageStates,$output);
return $output;
}
以上在實(shí)際操作中還是比較有用的,比如你不想用大組建,可以直接將變量輸?shù)侥0?也可以將多個(gè)變量組成數(shù)組輸?shù)侥0胬锩嫒?
- YII Framework的filter過濾器用法分析
- PHP的Yii框架中過濾器相關(guān)的使用總結(jié)
- Yii凈化器CHtmlPurifier用法示例(過濾不良代碼)
- Yii控制器中操作視圖js的方法
- 詳解PHP的Yii框架中的Controller控制器
- Yii使用find findAll查找出指定字段的實(shí)現(xiàn)方法
- 從零開始學(xué)YII2框架(五)快速生成代碼工具 Gii 的使用
- Yii框架form表單用法實(shí)例
- Yii查詢生成器(Query Builder)用法實(shí)例教程
- yii2.0之GridView自定義按鈕和鏈接用法
- Yii控制器中filter過濾器用法分析
相關(guān)文章
根據(jù)key刪除數(shù)組中指定的元素實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄鶕?jù)key刪除數(shù)組中指定的元素實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
分享下php5類中三種數(shù)據(jù)類型的區(qū)別
這篇文章主要介紹了php5類中三種數(shù)據(jù)類型的區(qū)別,需要的朋友可以參考下2015-01-01
PHP+MYSQL會(huì)員系統(tǒng)的開發(fā)實(shí)例教程
這篇文章主要介紹了PHP+MYSQL會(huì)員系統(tǒng)的開發(fā)實(shí)例教程,通過一個(gè)完整的會(huì)員系統(tǒng)開發(fā),進(jìn)一步加深對(duì)PHP+MySQL程序設(shè)計(jì)流程的認(rèn)識(shí),需要的朋友可以參考下2014-08-08
同臺(tái)服務(wù)器使用緩存APC效率高于Memcached的演示代碼
之前看到有文章說同臺(tái)服務(wù)器上APC的效率是Memcached的7倍,APC效率比Memcached高是肯定的,至于倒底快多少,我寫了個(gè)小程序測(cè)試了下。2010-02-02
php中運(yùn)用http調(diào)用的GET和POST方法示例
調(diào)用的GET和POST方法,使用到的函數(shù)是curl_init, curl_setopt, curl_exec,curl_close,默認(rèn)是GET方法2014-09-09

