现在我这样做了:
public function indexAction() {
...
$view = new ViewModel(array(
'foo' => 'bar',
));
return $view;
}
问题是我想在$ view渲染之后和布局渲染之前做一些事情:
public function indexAction() {
...
$view = new ViewModel(array(
'foo' => 'bar',
));
$layout = $this->layout();
$layout->content = $view->render();
...
// here I want to do some important action
...
$html = $layout->render();
return $this->getResponse()->setContent($html);
}
但是没有方法render()。在ZF1中,我可以呈现视图:
$view = new Zend_View($data);
$html = $view->render($templateName);
我怎样才能在ZF2中做到这一点?
答案 0 :(得分:28)
试试这个:
public function IndexAction()
{
...
$viewRender = $this->getServiceLocator()->get('ViewRenderer');
$html = $viewRender->render($viewModel);
...
}
答案 1 :(得分:14)
补充答案,包括布局......
public function IndexAction() {
...
$viewRender = $this->getServiceLocator()->get('ViewRenderer');
$layout = new ViewModel();
$layout->setTemplate("layout/main");
$layout->setVariable("content", $viewRender->render($viewModel));
$html = $viewRender->render($layout);
...
}