ZF2 view rendering

2019-06-14 10:23发布

问题:

In ZF1 I can do something like this:

$view = new Zend_View();
$view->setScriptPath($viewDir);
$html = $view->render('template_name.phtml');

How can I do that in ZF2?

回答1:

public function abcAction()
{
    $view = new ViewModel(array('variable'=>$value));
    $view->setTemplate('module/controler/action.phtml'); // path to phtml file under view folder
    return $view;
}

 public function differentViewScriptAction()
    {
        // Use a different view script

        $viewModel = new ViewModel();
        $viewModel->setTemplate('application/view/arbitrary');
        return $viewModel;
    }

Thanks to akrabat for covering almost every scenario.