zf2:嵌套视图模型问题

时间:2013-06-18 09:00:43

标签: zend-framework2

我正在尝试使用zf2创建嵌套视图模型。

我正在使用标准的zf2骨架应用程序。

IndexController

public function indexAction()
{
    $view = new ViewModel();
    $view->setTemplate('application/index/index.phtml');

    $aboutView = new ViewModel();
    $aboutView->setTemplate('application/index/about.phtml'); //standard html

    $view->addChild($aboutView, 'about');

    return $view;
}

在我的layout.phtml中,我添加了以下代码:

HTML code:

echo $this->content

HTML code:

echo $this->about;

结果中未显示嵌套视图。在var_dump($this->about)时,我收到了NULL.

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

你没有正确使用它。

<强> layout.phtml

<?php
/**
 * $view will be assigned to this, with the template index.phtml
 */
echo $this->content 

$ aboutView将仅作为子项分配给名为$ view的ViewModel。要访问它,您需要使用index.phtml

<强> index.phtml

<?php 
/**
 * This will have the content from about.phtml
 */
var_dump($this->about)

如果要将ViewModel分配给实际的基本ViewModel(使用layout.phtml),可以通过布局访问它:

public function testAction()
{
    $aboutView = new ViewModel();
    $aboutView->setTemplate('application/index/about.phtml'); //standard html
    $this->layout()->addChild($aboutView, 'about');

    //$this->layout() will return the ViewModel for the layout :)
    //you can now access $this->about inside your layout.phtml view file.
}
相关问题