Yii中render和renderPartial的區(qū)別
以下由我們在信易網(wǎng)絡公司開發(fā)項目的時候終結出的一些經驗
在進行頁面輸出渲染的時候。
1.render 輸出父模板的內容,將渲染的內容,嵌入父模板。|
2.renderPartial 則不輸出父模板的內容。只對本次渲染的局部內容,進行輸出。
同時還有個重要的區(qū)別:
render 函數(shù)內部默認執(zhí)行processOutput($output)函數(shù), 會將把組件,比如 CTreeView 里面注冊到 CClientScript 里面的
需要的腳本進行渲染輸出。
而renderPartial() 默認不自動渲染輸出客戶端腳本,需要進行參數(shù)的指定,才會輸出:
renderPartial($view,$data=null,$return=false,$processOutput=false)
指定processOutput 為 true 即可。
比如要局部輸出 CTreeView ,用renderPartial 進行渲染,如果按照默認processOutput=false 則輸出內容,不含有客戶端腳本
輸出內容則為 正常的 ul 列表。沒有樹形的折疊效果。 主動設定 processOutput=true 后,CTreeView 所需的,所有客戶端腳本就會被正常輸出在列表的前面。
下面介紹下要用到的幾個相關的函數(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ù)侥0?也可以將多個變量組成數(shù)組輸?shù)侥0胬锩嫒?
相關文章
php array_merge下進行數(shù)組合并的代碼
array_merge();合并兩個數(shù)組,如果數(shù)組的鍵名是字符,而且兩個數(shù)組的鍵名是相同的字符,2008-07-07Windows下利用Gvim寫PHP產生中文亂碼問題解決方法
昨天本來說早點睡覺呢,沒想到一個在寫PHP程序的時候發(fā)現(xiàn)出現(xiàn)的亂碼問題又睡晚了,不過總算解決了。2011-04-04php中session_unset與session_destroy的區(qū)別分析
關于session_unset()和session_destroy()的函數(shù)使用進行介紹。學習php的朋友可以參考下。2011-06-06phpmyadmin提示The mbstring extension is missing的解決方法
這篇文章主要介紹了phpmyadmin提示The mbstring extension is missing的解決方法,分析了錯誤提示的原因與不同平臺的解決方法,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12php self,$this,const,static,->的使用
用php這么久了,慚愧的是,原來自己還一直沒分清楚這幾個關鍵字使用方法。2009-10-10