-- James Andrews <[EMAIL PROTECTED]> wrote
(on Sunday, 27 May 2007, 08:02 AM -0400):
> I'm reading the manual on MVC and playing around a little to see how  
> it all works, and one thing I am failing to figure out how to do is  
> separation of common items from pages, such as headers, and footers,  
> and side bars.  Is it possible?  If so can someone point me in the  
> right direction, and if it is not why? I would expect that separation  
> like that is so common in sites that it should be.

You're talking about a Two Step View: get the body content (a rendered
view), and inject it into another view (site template, for example).

I use a dispatchLoopShutdown() plugin to do this. The easiest way is to
pull the content from the response object, inject it into a view, and
then set this rendered content as the response body content. As a
simplified example:

    <?php
    /** Zend_Controller_Plugin_Abstract */
    require_once 'Zend/Controller/Plugin/Abstract.php';

    /**
     * Dispatch loop shutdown plugin
     */
    class SiteTemplate extends Zend_Controller_Plugin_Abstract
    {
        public function dispatchLoopShutdown()
        {
            // get view
            $viewRenderer = 
Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer');
            $view         = $viewRenderer->view;
            $front        = Zend_Controller_Front::getInstance();
            $response     = $front->getResponse();
            $body         = $response->getBody();

            $view->content = $body;
            $response->setBody($view->render('site.phtml'));
        }
    }

You'll likely want to modify it a bit, but hopefully you get the general
idea.

> Also,
> 
> Say I want to have multiple views, working from the same controllers,  
> I see that the "Zend_View_Interface"  has a setScriptPath method, how  
> would you access this prior to displaying so that you can dynamically  
> choose new view scripts but keep the same controllers?

With the view renderer, what I'd do is, in your bootstrap, set a
'sitewide' view script path for view scripts that you want shared across
controllers:

$view = new Zend_View(array('basePath' => '../views'));
Zend_Controller_Action_HelperBroker::addHelper(new 
Zend_Controller_Action_Helper_ViewRenderer($view));

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to