yii $this->renderPartial()和使用$this->layout=false的$this->render()之间的差异

unguejic  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(137)

renderPartial和render with layout false之间有什么区别?我知道renderPartial不会包含layout。
$this->renderPartial()$this->layout=false; $this->render();的比较

g6baxovj

g6baxovj1#

不多。render()在内部使用renderPartial(),如果设置了,则将其 Package 在$layout中。
看来源:

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()afterRender(); renderPartial()则不会。
    1.在具有多个局部视图的场景中,renderPartial()将永远不会呈现任何$layout;如果在任何局部视图中设置了$layout,则render()将显示。

相关问题