« Return to Thread: Controller and View Question

Re: Controller and View Question

by Dale McNeill :: Rate this Message:

Reply to Author | View in Thread

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; ?>
</head>
<body>
...common header...
<?php echo $this->content; ?>
...common footer...
</body>
</html>

Matthew Weier O'Phinney wrote:

> -- Arnaud Limbourg <arnaud@...> 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'));
>     }
>
>  

--
------------------------------------------------------------------------
Dale McNeill                  |
Alchemy Systems               | phone: (512) 532-8050
http://www.alchemysystems.com | email: dale.mcneill@...
------------------------------------------------------------------------

 « Return to Thread: Controller and View Question