Yii 中,render 和 renderPartial 的区别。

进行页面输出渲染的时候。

render 输出父模板的内容,将渲染的内容,嵌入父模板。|
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 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))); }

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值