$this->renderPartial() 和 $this->render() 使用 $this->layout=false 的区别

Difference between $this->renderPartial() and $this->render() using $this->layout=false

renderPartial 和 render with layout false 有什么区别? 我知道 renderPartial 不会包括布局。

$this->renderPartial() 对比 $this->layout=false; $this->render();

不多。 render() 在内部使用 renderPartial() 并将其包装在 $layout 中(如果已设置)。

看看 source:

public function 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;
    }
}

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)));
}

我能看到的三个区别是:

  1. render()$layout = false 将 运行 processOutput()renderPartial() 除非您明确设置它,否则不会。
  2. render() 呼叫 beforeRender() and afterRender()renderPartial() 没有。
  3. 在有多个局部视图的场景中,renderPartial() 永远不会渲染任何 $layout;如果在任何局部视图中设置了 $layoutrender() 将会。