在zf2開發中, 經常接觸的view層大致分爲兩個方面, 佈局(layout.phtml)和內容部分(test.phtml), 本文介紹如何更換不同的佈局和內容模板.
默認模式如下所示.
//class TestController ..
public function testAction()
{
//對應到 view/layout/layout.phtml
//對應到 view/test/test.phtml
return array();
}
public function testAction()
{
//對應到 view/layout/test.phtml, 需在module.config.php 中配置
$layout = $this->layout();
$layout->setTemplate('layout/test');
//對應到 view/test/test1.phtml, 需在module.config.php 中配置
$viewModel = new ViewModel();
$viewModel->setVariables(array('err' => 'aaa'));
$viewModel->setTemplate('test/test1');
//返回值一定不是 return array();
return $viewModel;
}
希望對大家有幫助.