Yii控制层处理结果返回前端的三种方式
作者:zccst
批注: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
如果您觉得本文的内容对您的学习有所帮助,您可以微信: