1

This is a ZF2 question.

I'm trying to change my template, depending on a variable setted on my controller (since is there that im going to decide which template use). In my module onBooststrap i have:

$this->eventManager->attach('dispatch', function($e)
    {
        if (0 === strpos($e->getRouteMatch()->getParam('controller'), __NAMESPACE__, 0))
        {
            $e->getViewModel()->setTemplate('layout');
        }

    }, -100);

and in my controller:

class IndexController extends AbstractActionController
{
    public function indexAction ()
    {
        $view = new \Zend\View\Model\ViewModel();
        $view->setVariable("layout", "layout");
        return $view;
    }
}

but, how do i get access to that view variable "layout", so i can change it in the dispatch event on setTemplate?

Stoyan Dimov
  • 5,250
  • 2
  • 28
  • 44
MGP
  • 653
  • 1
  • 14
  • 33

1 Answers1

2

Looking at the onDispatch method and the MvcEvent class it seems there is a getResult() method, this may contain the result from the controller action.

Otherwise have you looked at the layout controller plugin? This plugin would allow you to change the template:

$this->layout('new layout');

Of course this would need to be inside the controller and most likely require adding the template into the template map.

DrBeza
  • 2,241
  • 1
  • 16
  • 18