yii中的render、renderPartial、renderText區別

看了CController.php中的解釋明白了。

/**
* Renders a view with a layout.
*
* This method first calls renderPartial} to render the view (called content view).
* It then renders the layout view which may embed the content view at appropriate place.
* In the layout view, the content view rendering result can be accessed via variable
* <code>$content</code>. At the end, it calls processOutput} to insert scripts
* and dynamic contents if they are available.
*
* By default, the layout view script is "protected/views/layouts/main.php".
* This may be customized by changing layout}.
*
* @param string name of the view to be rendered. See getViewFile} for details
* about how the view script is resolved.
* @param array data to be extracted into PHP variables and made available to the view script
* @param boolean whether the rendering result should be returned instead of being displayed to end users.
* @return string the rendering result. Null if the rendering result is not required.
* @see renderPartial
* @see getLayoutFile
*/
public function render($view,$data=null,$return=false)
{
   $output=$this->renderPartial($view,$data,true);
   if(($layoutFile=$this->getLayoutFile($this->layout))!==false)
    $output=$this->renderFile($layoutFile,array('content'=>$output),true);

   $output=$this->processOutput($output);

   if($return)
    return $output;
   else
    echo $output;
}

/**
* Renders a static text string.
* The string will be inserted in the current controller layout and returned back.
* @param string the static text string
* @param boolean whether the rendering result should be returned instead of being displayed to end users.
* @return string the rendering result. Null if the rendering result is not required.
* @see getLayoutFile
*/
public function renderText($text,$return=false)
{
   if(($layoutFile=$this->getLayoutFile($this->layout))!==false)
    $text=$this->renderFile($layoutFile,array('content'=>$text),true);

   $text=$this->processOutput($text);

   if($return)
    return $text;
   else
    echo $text;
}

/**
* Renders a view.
*
* The named view refers to a PHP script (resolved via getViewFile})
* that is included by this method. If $data is an associative array,
* it will be extracted as PHP variables and made available to the script.
*
* This method differs from render()} in that it does not
* apply a layout to the rendered result. It is thus mostly used
* in rendering a partial view, or an AJAX response.
*
* @param string name of the view to be rendered. See getViewFile} for details
* about how the view script is resolved.
* @param array data to be extracted into PHP variables and made available to the view script
* @param boolean whether the rendering result should be returned instead of being displayed to end users
* @param boolean whether the rendering result should be postprocessed using processOutput}.
* This parameter should be set true if renderPartial is the only method used to generate
* the output when handling a user request.
* @return string the rendering result. Null if the rendering result is not required.
* @throws CException if the view does not exist
* @see getViewFile
* @see processOutput
* @see render
*/
public function 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
    throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
     array('{controller}'=>get_class($this), '{view}'=>$view)));
}
上面說的很清楚了,render是調用layout渲染一個view,並顯示出來。renderpartial直接顯示一個view。renderText是顯示一個靜態的string字符串

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章