-- Ralph Schindler <
ralph@...> wrote
(on Sunday, 25 March 2007, 09:37 AM -0500):
> I am curious how everyone implements common headers and footers
> within a given application. As I see it there are two methods:
>
> a) include the header and footer in each view script via the
> $this->render(..) routine.
>
> b) use Zend_Controller Plugins to write to the view a standard header
> and footer to the view body at preDispatch() postDispatch() /
> preRouteStartup / postRouteStartup times.
>
> Both methods have their pros and cons..
>
> In method a) you are required to handle it in every script, which can be
> tedious. But it allows you flexibility to not include the common header
> and footer blanketly on all requests when it makes sense not to include it.
>
> Method b) writes headers and footers on all requests, unless I am
> missing something. The benefit is that you can write specific
> controller/action scripts and they can be completely autonomous from the
> rest of the application.
>
> I like method b), but I wonder: Are there ways to inject (or callback)
> to write specific controller/action needed headers to the common header,
> for example, adding a page specific JS script to a pages <head>. Are
> there ways that a specific controller/action can choose not to use a
> header/footer dispatched previously?
I throw a Zend_View object in the registry, and then access this from my
controllers and plugins. The benefit of doing this is that the
controllers can set values in the view that are unused in their
individual view, but used later in the sitewide template.
Then, I use a dispatchLoopShutdown() plugin to inject any generated
content into a sitwide template:
class SiteTemplatePlugin extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopShutdown()
{
$response = Zend_Controller_Front:;getInstance()->getResponse();
$view = Zend_Registry::get('view');
$view->content = $response->getBody();
$response->setBody($view->render('site.phtml'));
}
}
Any other variables you've set in your template object to this point are
also available, not just $content. As a result, you could in your action
controllers specify additional values:
public function formAction()
{
// Set the sitewide template's $pageTitle element
$this->view->pageTitle = 'Login Form';
$this->render();
}
By doing the sitewide template at the end, instead of doing a header in
the preDispatch() and footer in the postDispatch(), you can affect the
entire layout of the page, and also chain together several actions to
build the final page.
--
Matthew Weier O'Phinney
PHP Developer |
matthew@...
Zend - The PHP Company |
http://www.zend.com/