Yii render和renderPartial的区别
以下由我们在信易网络公司开发项目的时候终结出的一些经验
在进行页面输出渲染的时候。
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;
}
以上在实际操作中还是比较有用的,比如你不想用大组建,可以直接将变量输到模板,也可以将多个变量组成数组输到模版里面去
本文由专注于成都网站建设的信易网络发布,更多关于yii的信息请关注信易网络随后的发布,信易网络的官网http://www.ir58.com
Yii render和renderPartial的区别的更多相关文章
- yii中渲染模板时render与renderPartial的区别
render方法在渲染模板时会将渲染布局文件,而renderPartial则不会渲染布局
- [ASP.NET MVC]@Partial 和@RenderPartial的区别
@Partial 和@RenderPartial的区别 Html.partial和RenderPartial的用法与区别 Html.partial和RenderPartial都是输出html片段,区别 ...
- yii2 render和renderPartial区别
1.render()方法使用到项目中的布局layout,renderPartial()不使用布局
- Yii2 使用json 和设置component 中'format' => yii\web\Response::FORMAT_JSON 的区别
在Yii2中如果设置了 'response' => [ 'format' => yii\web\Response::FORMAT_JSON, 'charset' => 'UTF- ...
- rails中render 和 redirect_to的区别, each只能用在数组中,如果只有一个或者零个项,用each方法会报错undefined method `each' for #...
在render中,即使有:action,那么也仅仅是取对应的view中的模板(html.erb)而已,所以这里即使浏览器中的url是/orders/xcreate,但是显示的界面是/app/views ...
- Rails中render和redirect_to的区别
共同点: render 和redirect_to 都是执行页面跳转,但是,写在这两个方法后面的语句仍然会被执行. 不同: render:简单的页面渲染,可以指定渲染的页面或布局文件,但是不会发出请求, ...
- 普通组件定义渲染和render渲染组件的区别(三)
普通方式定义组件和效果: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- yii 验证器和验证码
http://www.yiiframework.com/doc/api/1.1/CCaptcha http://www.cnblogs.com/analyzer/articles/1673015.ht ...
- 关于 yii 验证码显示, 但点击不能刷新的处理
先说说 render 与 renderPartial, 各位看官, 先别走, 我没跑题, 这个问题如果需要解决, 关键就在 render 与 renderPartial 的区别. renderPart ...
随机推荐
- 【JAVA - SSM】之MyBatis逆向工程的使用
MyBatis逆向工程可以方便的从数据库中将表自动映射到JAVA POJO类,并同时生成Mapper.xml和Mapper接口,方便实用.下面介绍一下逆向工程的使用方法. 使用逆向工程,我们最好是新建 ...
- jqgrid使用简单记录
我要为id为jqGrid的table使用jqgrid插件. $("#jqGrid").jqGrid({ url: 'data/test.json', mtype: "GE ...
- 误删/tmp导致hadoop无法启停, jsp无法查看的解决方法
问题描述 我的hadoop版本是hadoop-cdh4.2.0,由于误删了/tmp目录(不是hadoop.tmp.dir设定的那个目录),在Namenode,SecondaryNamenode和Dat ...
- MVC - HtmlHelper类
传统的Html元素不能和服务端数据进行绑定 HtmlHelper类提供了一系列的方法来生成Html元素 并可以实现与数据绑定在一起 然后生成Html Html.BeginForm(actionName ...
- iOS之KVO和KVC
概述 由于ObjC主要基于Smalltalk进行设计,因此它有很多类似于Ruby.Python的动态特性,例如动态类型.动态加载.动态绑定等.今天我们着重介绍ObjC中的键值编码(KVC).键值监听( ...
- 深入理解计算机系统第二版习题解答CSAPP 2.8
给出位微量的布尔去处的求值结果. 运算 结果 a 0110 1001 b 0101 0101 ~a 1001 0110 ~b 1010 1010 a&b 0100 0001 a ...
- Nginx高性能服务器安装、配置、运维 (4) —— Nginx服务、架构及其信号
五.Nginx服务.架构及其信号 (1)Nginx服务的查看 1.netstat -antp 查看Nginx是否在80端口运行: 2.ps aux|grep nginx 查看nginx相关进程: 发现 ...
- MySQL数据库分表的3种方法
原文地址:MySQL数据库分表的3种方法作者:dreamboycx 一,先说一下为什么要分表 当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目 ...
- 整理SVN代码-->正式环境的代码
最近我被分配到了合并正式补丁代码的工作.聊聊整个流程 第一步解压补丁
- (转)Ilist 和list的区别归纳总结
常见问题: Ilist <> 本身只是一个泛型接口, 既然是接口当然不能实例化,只能用如下方法 IList <Class1> IList11 =new List <Cla ...