Can I render view without returning it in action controller in zend framework 2?

前端 未结 2 1336
北荒
北荒 2021-02-09 07:15

Now I am doing so:

public function indexAction() {
    ...
    $view = new ViewModel(array(
        \'foo\' => \'bar\',
    ));
    return $view;
}

2条回答
  •  梦如初夏
    2021-02-09 07:36

    Complementing the answer, with layout included...

    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);
        ...
    }
    

提交回复
热议问题