-- Dale McNeill <[EMAIL PROTECTED]> wrote
(on Tuesday, 27 March 2007, 09:19 AM -0500):
> I've got some CSS and javascript that I would like to dynamically add to 
> the HTML header depending on the controller/action.  I would like to be 
> able to append information like appending to the response body.  Then 
> use this information in a site wide template.  The only solution that 
> comes to mind is using a view variable and having each controller/action 
> get the variable, append it, and write it back to the view.  Is there 
> some functionality that I might be overlooking? - Thanks
> 
> Site wide template:
> <html>
> <head>
> <title> <?php echo $this->title; ?> </title>
> <?php echo $this->dynamic_header; ?>

Define $dynamic_header as an array in the view object. When you first
initialize the view object, do something like this:

    $view = new Zend_View();
    $view->dynamic_header = array();

Then, whenever you want to add to it, just add a new element to the
array:

    $view->dynamic_header[] = '<meta name="keywords" value="zend framework 
zend_view" /> ';

Then, in the view script, iterate over the array:

    <?php foreach ($this->dynamic_header as $header): 
            echo $header, "\n";
    endforeach; ?>

Finally, use a Two Step View as I've outlined previously in this thread
-- use a dispatchLoopShutdown() plugin to throw the response body into a
sitewide template (which it looks like you're doing here).

> </head>
> <body>
> ...common header...
> <?php echo $this->content; ?>
> ...common footer...
> </body>
> </html>
> 
> Matthew Weier O'Phinney wrote:
> > -- Arnaud Limbourg <[EMAIL PROTECTED]> wrote
> > (on Monday, 26 March 2007, 07:04 AM +0200):
> >  
> > > Matthew Weier O'Phinney wrote:
> > >    
> > > > 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'));
> > > >       }
> > > >   }
> > > >      
> > > Which poses a problem when you want to send back json (or whatever) and 
> > > you don't want a site wide template :)
> > >    
> >
> > This was a simple example. But it's actually really easy to return JSON:
> >
> >    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', false)) {
> >            // 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
> >            $resposne->setHeader('Content-Type', 'text/x-json');
> >            $response->setBody(Zend_Json::encode($vars));
> >            return;
> >        }
> >
> >        // Otherwise, process as normal
> >        $view->content = $response->getBody();
> >        $response->setBody($view->render('site.phtml'));
> >    }
-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to