1

I seem to have a problem with my layout displaying twice. I was wondering if this was caused by something in my front controller plugin (see below) which utilises Zend Layout only for a particular module and not in the main application. Any answers will be much appreciated. Thanks

class My_Controller_Plugin_AdoptLayout extends Zend_Controller_Plugin_Abstract

{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {

            $moduleName = $request->getModuleName();                

            if ($moduleName == 'some-module') {

                    if (!Zend_Layout::isEnabled()) {

                            $view = new Zend_View(array('encoding' => 'UTF-8'));
                            $view->addScriptPath(ROOT_DIR . '/modules/some-module/views/scripts/');
                        $view->doctype('XHTML1_STRICT');

                            $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
                        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);  

                            Zend_Layout::startMvc();
                            $layout = Zend_Layout::getMvcInstance();
                            $layout->setLayout('default');
                            $layout->setLayoutPath(APPLICATION_PATH . '/modules/some-module/views/scripts/layouts/');
                            $layout->setView($view);                        
                    }

            }                                                           
    }

}

3 Answers3

2

This is typically caused by the ErrorController in my experience. I usually clear the response to make sure there's no nesting:

public function errorAction() {

        $this->getResponse()->clearBody();

//handle error ........

}
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
1

I have run into this problem when using _forward or when I redirected to the errorController within a preDispatch plugin. You could check for these things.

Also i'm a little suspicious about the line:

$layout->setView($view);

Is it necessary. Doesn't Zend automatically assign the view to the layout.

andho
  • 1,166
  • 1
  • 15
  • 27
  • Thanks andho. I wondered if it may be _forward actually but I really need to use it as I have to pass form values between actions and can't seem to get it to work with $this->_helper->redirector. I had to create a new view object because the main application uses a Zend_View/Smarty hybrid and I don't want to change all my templates as they are pure Zend_View. – somekindofusernamegoeshere Sep 25 '09 at 14:15
  • Why would you need to pass form data to another action? Is the action in the same Controller. If so can't you call $this->otherAction(); Or use action chaining but this might potentially render another view unless you switch off rendering for either one of the actions – andho Sep 25 '09 at 14:24
0

If you call an action from an Ajax call and your layout gets called twice, you could set

$this->_helper->layout->disableLayout();

in the Action.

At my project I also made a wrong ajax call, which made it render another time. Hope this helps

Joris Kok
  • 113
  • 12