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

Asg*_*Ali 2 yii

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

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

top*_*her 5

不多.内部render()使用renderPartial()并在$layoutif集中包装它.

看看来源:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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)));
}
Run Code Online (Sandbox Code Playgroud)

我能看到的三个不同之处是:

  1. render()$layout = false运行processOutput(); renderPartial()除非你明确地设置它,否则不会.
  2. render()电话beforeRender()afterRender(); renderPartial()才不是.
  3. 在具有多个部分视图的场景中,renderPartial()永远不会渲染任何部分$layout; render()将if $layout设置在任何部分视图中.