yii 中render 和renderpartial的使用

render 和renderpartial之间最大的区别就是:一个是渲染模板,一个不渲染模板。

其中render 输出父模板的内容,将渲染的内容,嵌入父模板。

renderPartial 则不输出父模板的内容。只对本次渲染的局部内容,进行输出。

render函数的说明如下:

public function render( v i e w , view, view,data=null,KaTeX parse error: Expected '}', got 'EOF' at end of input: …alse) { if(this->beforeRender($view))
{
o u t p u t = output= output=this->renderPartial( v i e w , view, view,data,true);//渲染子模板
if(( l a y o u t F i l e = layoutFile= layoutFile=this->getLayoutFile($this->layout))!==false)

//将子模版渲染的内容放到content变量中去渲染父模板,在父模板中输出$content
o u t p u t = output= output=this->renderFile( l a y o u t F i l e , a r r a y ( ′ c o n t e n t ′ = > layoutFile,array('content'=> layoutFile,array(content=>output),true);

    $this->afterRender($view,$output);

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

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

}

renderpartial函数的说明如下:

public function renderPartial( v i e w , view, view,data=null, r e t u r n = f a l s e , return=false, return=false,processOutput=false)
{
if(( v i e w F i l e = viewFile= viewFile=this->getViewFile($view))!==false)
{
o u t p u t = output= output=this->renderFile( v i e w F i l e , viewFile, viewFile,data,true);
if($processOutput)
o u t p u t = output= output=this->processOutput( o u t p u t ) ; i f ( output); if( output);if(return)
return $output;
else
echo KaTeX parse error: Expected 'EOF', got '}' at position 13: output; }̲ else …this), ‘{view}’=>$view)));
}

通过观察可知,render函数内部默认执行processOutput()函数,而renderpartial函数必须制定才会执行。

我们经常使用的系统,通常头部和底部是相同的,这个时候可以使用布局渲染,每个页面只需要使用这个布局,然后填上中间自己的部分,这样的好处是在修改头部和底部的时候,不用每个页面都修改,只要修改相应的布局页面就可以了。

头部的代码
<?php echo $content;?> //替换相应的内容
底部的代码

使用的布局的时候,只需要在使用render函数,同时设置布局使用的文件,就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值