当前位置: 移动技术网 > IT编程>开发语言>PHP > Yii中render和renderPartial的区别

Yii中render和renderPartial的区别

2018年07月20日  | 移动技术网IT编程  | 我要评论

成都玉林中学宏志班,蓝雪魔域,狂刀卡盟

以下由我们在信易网络公司开发项目的时候终结出的一些经验
在进行页面输出渲染的时候。

1.render 输出父模板的内容,将渲染的内容,嵌入父模板。|
2.renderpartial 则不输出父模板的内容。只对本次渲染的局部内容,进行输出。

同时还有个重要的区别:

render 函数内部默认执行processoutput($output)函数, 会将把组件,比如 ctreeview 里面注册到 cclientscript 里面的
需要的脚本进行渲染输出。

而renderpartial() 默认不自动渲染输出客户端脚本,需要进行参数的指定,才会输出:
renderpartial($view,$data=null,$return=false,$processoutput=false)
指定processoutput 为 true 即可。

比如要局部输出 ctreeview ,用renderpartial 进行渲染,如果按照默认processoutput=false 则输出内容,不含有客户端脚本
输出内容则为 正常的 ul 列表。没有树形的折叠效果。 主动设定 processoutput=true 后,ctreeview 所需的,所有客户端脚本就会被正常输出在列表的前面。

下面介绍下要用到的几个相关的函数:

render,renderpartial 不再介绍
processoutput()

<?php
publicfunction 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;
  }
}
publicfunction 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
    thrownewcexception(yii::t('yii','{controller} cannot find the requested view "{view}".',
      array('{controller}'=>get_class($this),'{view}'=>$view)));
}
publicfunction processoutput($output)
{
  yii::app()->getclientscript()->render($output);
  // if using page caching, we should delay dynamic output replacement
  if($this->_dynamicoutput!==null&& $this->iscachingstackempty())
  {
    $output=$this->processdynamicoutput($output);
    $this->_dynamicoutput=null;
  }
  if($this->_pagestates===null)
    $this->_pagestates=$this->loadpagestates();
  if(!empty($this->_pagestates))
    $this->savepagestates($this->_pagestates,$output);
  return $output;
}

以上在实际操作中还是比较有用的,比如你不想用大组建,可以直接将变量输到模板,也可以将多个变量组成数组输到模版里面去.

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网