Yii中render和renderPartial的區別


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

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()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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 ;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM