2

我对 Zend Framework 2 有疑问。

我想为每个操作提供 2 个视图脚本,以显示适用于 PC 和智能手机的视图。我知道如何更改布局:

$viewModel->setTemplate(layout_path)

但我不知道如何更改视图脚本。

我在索引视图文件夹中有以下文件Application/view/application/index,用于IndexController

index.php  (view file for PC)

index_sp.php  (view file for smartphone)

如何index_sp.php在控制器或控制器插件中更改视图脚本?

4

2 回答 2

11

If you want to change the actual layout view file you can do this inside your controller:

// example to change base layout for ajax requests
if($this->getRequest()->isXmlHttpRequest()) {
    $this->layout('layout/ajax-layout');
}

If you want to change the view used by the current view model / action you can do this inside your controller/action:

$viewModel = new ViewModel(array(
    'form'      => $form
    'something' => $something
));
$viewModel->setTemplate('mymodule/newview.phtml');

return $viewModel;
于 2013-01-23T11:43:50.767 回答
1
public function doSomethingCrazyAction()    {
    $view = new ViewModel(array(
        'message' => 'Hello world',
    ));

    // This is the way to change render(view)..
    $view->setTemplate('modulename/controllername/phtml-file-name');

    return $view;
}

文档:http : //framework.zend.com/manual/current/en/modules/zend.view.quick-start.html

于 2015-02-05T10:10:25.897 回答