The way the Zend_View Enhanced proposal  (and also the Zend_View_Helper_* 
proposal) address this is to setup a centrally registered data store all View 
templates can access. Then any sub-templates which compose the end View can 
dynamically add/append/remove items. Both proposals add specific helpers for 
each of scripts, styles, title, etc. in the HEAD section.

Alternatively, if defining template by template seems confusing, one could use 
the same system in a more Java-esque way (think the Tiles tag library plugin 
for Struts) and define such upfront in a set of site specific template files 
which are included by the base View template only, or included by some other 
means into the sitewide layout template (if using the ZVE approach). These can 
then be injected in the layout template holding the HEAD section using simple 
echo's (all these helpers inplement __toString() and allow for very specific 
element ordering which is sometimes important for script inclusion).

<head>
<?php echo $this->headTitle() ?>
<?php echo $this->headStyle() ?>
<?php echo $this->headScript() ?>
</head>

The upfront configuration method is also useful in the case that you define 
which configuration to utilise as a View variable set by the base Controller. 
It would just need to be included early into the base View object so it's not 
overwriting any subtemplate alterations (which a user may choose to enforce 
since they can't be completely determined upfront for some reason).

Regards,
Pádraic
 
Pádraic Brady
http://blog.astrumfutura.com
http://www.patternsforphp.com


----- Original Message ----
From: Dr Livingston <[EMAIL PROTECTED]>
To: [email protected]
Sent: Saturday, July 14, 2007 9:07:50 PM
Subject: Re: [fw-general] Controller and View Question


What I do is to treat the HEAD as a separate View in it's self, so you can
use either a common template for site wide, or a more specific template for
any given request.

Thus, I can add Javascript, Meta Tags etc on demand, without effecting the
rest of the system. On another note, I think it's a bad idea,
architecturally, to be dependent on using plugins to aid in your output
generation.

The functionality and facilities should already be there, available in a
lower layer.


Matthew Weier O'Phinney-3 wrote:
> 
> -- 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/
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Controller-and-View-Question-tf3462561s16154.html#a11596858
Sent from the Zend Framework mailing list archive at Nabble.com.








       
____________________________________________________________________________________
Sick sense of humor? Visit Yahoo! TV's 
Comedy with an Edge to see what's on, when. 
http://tv.yahoo.com/collections/222

Reply via email to