The response object has support for "named segments". This allows you to segregate body content into different segments and order those segments so output is returned in a specific order. Internally, body content is saved as an array, and the various accessor methods can be used to indicate placement and names within that array.
As an example, you could use a preDispatch() hook to
add a header to the response object, then have the action controller
add body content, and a postDispatch() hook add a
footer:
<?php
// Assume that this plugin class is registered with the front controller
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$response = $this->getResponse();
$view = new Zend_View();
$view->setBasePath('../views/scripts');
$response->prepend('header', $view->render('header.phtml'));
}
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
$response = $this->getResponse();
$view = new Zend_View();
$view->setBasePath('../views/scripts');
$response->append('footer', $view->render('footer.phtml'));
}
}
// a sample action controller
class MyController extends Zend_Controller_Action
{
public function fooAction()
{
$this->render();
}
}
In the above example, a call to /my/foo will cause the
final body content of the response object to have the following
structure:
<?php
array(
'header' => ..., // header content
'default' => ..., // body content from MyController::fooAction()
'footer' => ... // footer content
);
When this is rendered, it will render in the order in which elements are arranged in the array.
A variety of methods can be used to manipulate the named segments:
setBody()andappendBody()both allow you to pass a second value,$name, indicating a named segment. In each case, if you provide this, it will overwrite that specific named segment or create it if it does not exist (appending to the array by default). If no named segment is passed tosetBody(), it resets the entire body content array. If no named segment is passed toappendBody(), the content is appended to the value in the 'default' name segment.prepend($name, $content)will create a segment named$nameand place it at the beginning of the array. If the segment exists already, it will be removed prior to the operation (i.e., overwritten and replaced).append($name, $content)will create a segment named$nameand place it at the end of the array. If the segment exists already, it will be removed prior to the operation (i.e., overwritten and replaced).insert($name, $content, $parent = null, $before = false)will create a segment named$name. If provided with a$parentsegment, the new segment will be placed either before or after that segment (based on the value of$before) in the array. If the segment exists already, it will be removed prior to the operation (i.e., overwritten and replaced).clearBody($name = null)will remove a single named segment if a$nameis provided (and the entire array otherwise).getBody($spec = false)can be used to retrieve a single array segment if$specis the name of a named segment. If$specisFALSE, it returns a string formed by concatenating all named segments in order. If$specisTRUE, it returns the body content array.




