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

前端 未结 2 1335
北荒
北荒 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);
        ...
    }
    
    0 讨论(0)
  • 2021-02-09 07:41

    Try this:

    public function IndexAction()
    {
        ...
        $viewRender = $this->getServiceLocator()->get('ViewRenderer');
        $html = $viewRender->render($viewModel);
        ...
    }
    
    0 讨论(0)
提交回复
热议问题