YII 返回视图3种方式
来源:互联网 发布:淘宝店铺首页怎么弄 编辑:程序博客网 时间:2024/06/07 18:51
批注:render会渲染layout,而renderPartial不会渲染。
一、渲染方式
1,局部渲染renderPartial
public function renderPartial($view,$data=null,$return=false,$processOutput=false)
{
if(($viewFile=$this->getViewFile($view))!==false)
{
$output=$this->renderFile($viewFile,$data,true);//getViewFile($view)获得$view的完整路径
if($processOutput)
$output=$this->processOutput($output); // processOutput()作用,比如在head加上css或js脚本等
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)));
}
注解:
(1)getViewFile($view)获得$view的完整路径
(2)如果没有在$config里配置第三方的renderer,renderFile() 里实际是调用了yii自身提供的renderInternal()来render view文件:
public function renderFile($viewFile,$data=null,$return=false)
{
$widgetCount=count($this->_widgetStack);
// 如果配置了其他的ViewRenderer
if(($renderer=Yii::app()->getViewRenderer())!==null)
$content=$renderer->renderFile($this,$viewFile,$data,$return);
else
// yii 自身的render
$content=$this->renderInternal($viewFile,$data,$return);
if(count($this->_widgetStack)===$widgetCount)
return $content;
else
{
$widget=end($this->_widgetStack);
throw new CException(Yii::t('yii','{controller} contains improperly nested widget tags in its view "{view}". A {widget} widget
does not have an endWidget() call.',
array('{controller}'=>get_class($this), '{view}'=>$viewFile, '{widget}'=>get_class($widget))));
}
}
Yii的renderer用的是php本身作为模板系统:
public function renderInternal($_viewFile_,$_data_=null,$_return_=false)
{
// extract函数将$_data_从数组中将变量导入到当前的符号表
if(is_array($_data_))
extract($_data_,EXTR_PREFIX_SAME,'data');
else
$data=$_data_;
if($_return_)
{
ob_start();
ob_implicit_flush(false);
require($_viewFile_);
return ob_get_clean();
}
else
require($_viewFile_);
}
2,全局渲染render
render()的实际上是先renderPartial view文件,然后renderFile layoutfile,并将view文件的结果做为$content变量传入。
public function render($view,$data=null,$return=false)
{
$output=$this->renderPartial($view,$data,true);
if(($layoutFile=$this->getLayoutFile($this->layout))!==false)
$output=$this->renderFile($layoutFile,array('content'=>$output),true);
$output=$this->processOutput($output);
if($return)
return $output;
else
echo $output;
}
processOutput将render的结果再做处理,比如在head加上css或js脚本等。
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);
if($this->_pageStates===null)
$this->_pageStates=$this->loadPageStates();
if(!empty($this->_pageStates))
$this->savePageStates($this->_pageStates,$output);
return $output;
}
区别:
render会把需要的js,css等嵌入
renderPartial可以通过把最后一个参数设置成true完成一样的功能 $this->renderPartial('partial_view', $params, false, true);
实例:
在../controllers/XXController.php中
$this->render('update',array('model'=>$model));
在../views/ControllerID/update.php中
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
二、暴力跳转模式——适合form表单提交
$this->redirect(array('view','id'=>$model->id));
三、返回值方式——适合异步调用
//写法1:
public function response($data, $type="application/json"){
print json_encode($data);
Yii::app()->end();
}
//写法2:
$this->layout = false;
header('Content-type: application/json');
echo json_encode($arr);
Yii::app()->end(); //执行该行,已经将layout设为了false
一、渲染方式
1,局部渲染renderPartial
public function renderPartial($view,$data=null,$return=false,$processOutput=false)
{
if(($viewFile=$this->getViewFile($view))!==false)
{
$output=$this->renderFile($viewFile,$data,true);//getViewFile($view)获得$view的完整路径
if($processOutput)
$output=$this->processOutput($output); // processOutput()作用,比如在head加上css或js脚本等
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)));
}
注解:
(1)getViewFile($view)获得$view的完整路径
(2)如果没有在$config里配置第三方的renderer,renderFile() 里实际是调用了yii自身提供的renderInternal()来render view文件:
public function renderFile($viewFile,$data=null,$return=false)
{
$widgetCount=count($this->_widgetStack);
// 如果配置了其他的ViewRenderer
if(($renderer=Yii::app()->getViewRenderer())!==null)
$content=$renderer->renderFile($this,$viewFile,$data,$return);
else
// yii 自身的render
$content=$this->renderInternal($viewFile,$data,$return);
if(count($this->_widgetStack)===$widgetCount)
return $content;
else
{
$widget=end($this->_widgetStack);
throw new CException(Yii::t('yii','{controller} contains improperly nested widget tags in its view "{view}". A {widget} widget
does not have an endWidget() call.',
array('{controller}'=>get_class($this), '{view}'=>$viewFile, '{widget}'=>get_class($widget))));
}
}
Yii的renderer用的是php本身作为模板系统:
public function renderInternal($_viewFile_,$_data_=null,$_return_=false)
{
// extract函数将$_data_从数组中将变量导入到当前的符号表
if(is_array($_data_))
extract($_data_,EXTR_PREFIX_SAME,'data');
else
$data=$_data_;
if($_return_)
{
ob_start();
ob_implicit_flush(false);
require($_viewFile_);
return ob_get_clean();
}
else
require($_viewFile_);
}
2,全局渲染render
render()的实际上是先renderPartial view文件,然后renderFile layoutfile,并将view文件的结果做为$content变量传入。
public function render($view,$data=null,$return=false)
{
$output=$this->renderPartial($view,$data,true);
if(($layoutFile=$this->getLayoutFile($this->layout))!==false)
$output=$this->renderFile($layoutFile,array('content'=>$output),true);
$output=$this->processOutput($output);
if($return)
return $output;
else
echo $output;
}
processOutput将render的结果再做处理,比如在head加上css或js脚本等。
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);
if($this->_pageStates===null)
$this->_pageStates=$this->loadPageStates();
if(!empty($this->_pageStates))
$this->savePageStates($this->_pageStates,$output);
return $output;
}
区别:
render会把需要的js,css等嵌入
renderPartial可以通过把最后一个参数设置成true完成一样的功能 $this->renderPartial('partial_view', $params, false, true);
实例:
在../controllers/XXController.php中
$this->render('update',array('model'=>$model));
在../views/ControllerID/update.php中
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
二、暴力跳转模式——适合form表单提交
$this->redirect(array('view','id'=>$model->id));
三、返回值方式——适合异步调用
//写法1:
public function response($data, $type="application/json"){
print json_encode($data);
Yii::app()->end();
}
//写法2:
$this->layout = false;
header('Content-type: application/json');
echo json_encode($arr);
Yii::app()->end(); //执行该行,已经将layout设为了false
- YII 返回视图3种方式
- yii视图使用的widget方式总结
- yii 视图
- 【Yii】视图
- Yii 1.1版本控制器视图与ajax的使用方式
- 推荐 SpringMVC 的 Controller 返回各种视图的处理方式(3))
- yii视图布局文件
- yii 视图理解
- Yii 视图自定义
- yii 用户管理 视图
- Yii 视图渲染过程
- yii的视图
- Yii之视图
- SpringMVC 的 Controller 返回各种视图的处理方式
- SpringMVC 的 Controller 返回各种视图的处理方式
- SpringMvc的Controller返回各种视图的处理方式
- SpringMVC 的 Controller 返回各种视图的处理方式
- Yii 表单交互的三种方式
- J2SE常用包的含义
- HTML5 技术站点
- DataGridView.AutoGenerateColumns 属性
- Uva-11995-I Can Guess the Data Structure!
- 黑马程序员 笔记(九)——面向对象(内部类)
- YII 返回视图3种方式
- Oracle 使用RMAN COPY 移动 整个数据库 位置 示例
- Activity 启动模式
- indy组件在2010的重装
- jquery读取json格式
- 我来理解extern "C"
- GCC学习笔记
- 分享一个好用的免费查找PCB封装尺寸的软件工具
- 第三届蓝桥杯预赛java组第二题比酒量