zend framework 2 更換佈局模板(layout)和內容模板

在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;
	}

希望對大家有幫助.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章