问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/12588278/access-viewmodel-variables-on-dispatch-event