This article mainly introduces the differences between render and renderPartial in Yii, the following are some of the experiences we have gained when developing projects in Xinyi network. The following are some of the experiences we have gained when developing projects in Xinyi network.
During page output rendering.
1. render outputs the content of the parent template and embeds the rendered content into the parent template. |
2. renderPartial does not output the content of the parent template. Only the partial content of this rendering is output.
There is also an important difference:
The render function executes the processOutput ($ output) function by default, and registers components, such as
The required script for rendering and output.
RenderPartial () does not automatically render the output client script by default. you need to specify the parameters before the output:
RenderPartial ($ view, $ data = null, $ return = false, $ processOutput = false)
Set processOutput to true.
For example, to locally output the CTreeView, use renderPartial for rendering. if the default value is processOutput = false, the output content does not contain client scripts.
The output content is a normal ul list. There is no tree-like folding effect. After processOutput is set to true, all client scripts required by CTreeView will be normally output in front of the list.
The following describes the functions to be used:
Render, renderPartial not introduced
ProcessOutput ()
<?phppublicfunction 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;}
The above is useful in actual operations. for example, if you do not want to use a large component, you can directly input the variables to the template, or input multiple variables into an array.