There are two ways to render the view. One is to use action render helper:Zend_Controller_Action_Helper_ViewRenderer, The other way is to use render() method in the Zend_Controller_Action.
You may be curious of the difference between these two approaches. In my eyes, Zend_Controller_Action_Helper_ViewRenderer provides us a more helpful way to render the view.
You may be curious of the difference between these two approaches. In my eyes, Zend_Controller_Action_Helper_ViewRenderer provides us a more helpful way to render the view.
1.The logic in the Zend_Controller_Action's rende() method
public function render($action = null, $name = null, $noController = false)
{
if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
return $this->_helper->viewRenderer->render($action, $name, $noController);
}
$view = $this->initView();
$script = $this->getViewScript($action, $noController);
$this->getResponse()->appendBody(
$view->render($script),
$name
);
}
2.The magic power in Zend_Controller_Action_Helper_ViewRenderer
public function postDispatch()
{
if ($this->_shouldRender()) {
$this->render();
}
}
public function render($action = null, $name = null, $noController = null)
{
$this->setRender($action, $name, $noController);
$path = $this->getViewScript();
$this->renderScript($path, $name);
}
public function renderScript($script, $name = null)
{
if (null === $name) {
$name = $this->getResponseSegment();
}
$this->getResponse()->appendBody(
$this->view->render($script),
$name
);
$this->setNoRender();
}