Yii render和renderPartial的區別_PHP教程

來源:互聯網
上載者:User
以下由我們在信易網路公司開發項目的時候終結出的一些經驗

在進行頁面輸出渲染的時候。

1.render 輸出父模板的內容,將渲染的內容,嵌入父模板。|
2.renderPartial 則不輸出父模板的內容。只對本次渲染的局部內容,進行輸出。
同時還有個重要的區別:

render 函數內部預設執行processOutput($output)函數, 會將把組件,比如 CTreeView 裡面註冊到 CClientScript 裡面的
需要的指令碼進行渲染輸出。

而renderPartial() 預設不自動渲染輸出用戶端指令碼,需要進行參數的指定,才會輸出:
renderPartial($view,$data=null,$return=false,$processOutput=false)
指定processOutput 為 true 即可。

比如要局部輸出 CTreeView ,用renderPartial 進行渲染,如果按照預設processOutput=false 則輸出內容,不含有用戶端指令碼
輸出內容則為 正常的 ul 列表。沒有樹形的摺疊效果。 主動設定 processOutput=true 後,CTreeView 所需的,所有用戶端指令碼就會被正常輸出在列表的前面。

下面介紹下要用到的幾個相關的函數:
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;
}

以上在實際操作中還是比較有用的,比如你不想用大組建,可以直接將變數輸到模板,也可以將多個變數組成數組輸到模版裡面去

本文由專註於成都網站建設的信易網路發布,更多關於yii的資訊請關注信易網路隨後的發布,信易網路的官網http://www.ir58.com

http://www.bkjia.com/PHPjc/767672.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/767672.htmlTechArticle以下由我們在信易網路公司開發項目的時候終結出的一些經驗 在進行頁面輸出渲染的時候。 1.render 輸出父模板的內容,將渲染的內容,嵌...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.