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?
Looking at the
onDispatchmethod and theMvcEventclass it seems there is agetResult()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:
Of course this would need to be inside the controller and most likely require adding the template into the template map.