I use a dispatchLoopShutdown plugin. This plugin is able to deal with
both AJAX responses as well as a normal HTML response. Here's the code:
class SiteTemplatePlugin extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopShutdown()
{
// assume that we've already determined the request is ajax
$request = $this->getRequest();
$response = $this->getResponse();
$view = Zend_Registry::get('view');
if ($request->getParam('isAjax')) {
// Ajax request detected
// Get any variables set in the view
$vars = get_object_vars($view);
// Merge with named path segments in response
$vars = array_merge($vars, $response->getBody(true));
// Create a header and set the response body to a JSON value
$response->setHeader('Content-Type', 'text/x-json');
$response->setBody(Zend_Json::encode($vars));
return;
}
// Otherwise, process as normal
$view->assign($response->getBody(true));
$response->setHeader('Content-Type', 'text/html');
$response->setBody($view->render('site.php'));
}
}
The site wide template that I use is composed of a header, 3 columns,
and a footer. The header and footer are the same for each page. I
assign the content for each column by appending the response object with
a named section. The named sections eventually become the view
variables used in the site template. Here's an example of a normal HTML
response:
class IndexController extends Zend_Controller_Action
{
// Normal HTML response
public function indexAction()
{
$response = $this->getResponse();
$view = Zend_Registry::get('view');
$view->title = 'My Index Title';
$response->appendBody($view->render('index_css.php'), 'inline_css');
$response->appendBody($view->render('index_left.php'),
'left_content');
$response->appendBody($view->render('index_center.php'),
'center_content');
$response->appendBody($view->render('index_right.php'),
'right_content');
}
// AJAX response
public function viewAction()
{
$request = $this->getRequest();
$request->setParam("isAjax", true);
$view = Zend_Registry::get('view');
$view->status = 0;
$view->message = "Problem encounted";
}
}
Here's the site wide template:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $this->escape($this->title); ?></title>
<script src="/javascript/json.js"></script>
<style type="text/css">
<?php echo $this->inline_css; ?>
</style>
</head>
<body>
<div id="header">
<div id="header">This is the header.</div>
</div>
<div id="container">
<div id="center" class="column">
<?php echo $this->center_content; ?>
</div>
<div id="left" class="column">
<?php echo $this->left_content; ?>
</div>
<div id="right" class="column">
<?php echo $this->right_content; ?>
</div>
</div>
<div id="footer-wrapper">
<div id="footer">This is the footer.</div>
</div>
</body>
</html>
Hope that helps everyone.
Thanks,
Dale
Pádraic Brady wrote:
Hi all,
I've been having one of those long discussions about implementing the
Zend Framework in a sample application (sort of an exercise a few of
us are doing to improve our own practices and knowledge of working
with the ZF ;)).
When we came to rendering a web page, we assumed there would be lots
of reuseable widgets and elements included - not all that far fetched
really. The problem is that Zend_View isn't all that cooked up for the
task. I tried finding some references to a current practice but all I
seem to find is references to using a controller's _forward() method
to switch in and out of other controllers (which grab the Model and
View for reusable elements of a webpage).
I'm not sure I follow the logic of this - it seems to be something
that becomes prohibitively more complex the more reusable elements are
added - not to mention controller to controller knowledge is basically
coupling and seems more a case of an afterthought system than
something deliberate - especially unless it follow some configurable
pattern to dynamically build layouts.
Has anyone found, or can recommend, a simple easy to implement
practice? As it stands our discussion has leaned towards introducing
Layouts and subclassing Zend_View to introduce the Composite View
pattern (and possibly View Helpers to allow Views access the Model
read-only style). I can't find anything at present which offers a more
elegant solution for introducing reusable elements into an overall View.
Hopefully the wizards occupying this mailing list have some ideas :).
Best regards,
Pádraic
Pádraic Brady
http://blog.astrumfutura.com
http://www.patternsforphp.com
Ahhh...imagining that irresistible "new car" smell?
Check out new cars at Yahoo! Autos.
<http://us.rd.yahoo.com/evt=48245/*http://autos.yahoo.com/new_cars.html;_ylc=X3oDMTE1YW1jcXJ2BF9TAzk3MTA3MDc2BHNlYwNtYWlsdGFncwRzbGsDbmV3LWNhcnM->
--
------------------------------------------------------------------------
Dale McNeill |
Alchemy Systems | phone: (512) 532-8050
http://www.alchemysystems.com | email: [EMAIL PROTECTED]
------------------------------------------------------------------------