1

我正在尝试使用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 代码:

echo $this->content

HTML 代码:

echo $this->about;

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

知道我做错了什么吗?

4

1 回答 1

1

你没有正确使用它。

布局.phtml

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

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

索引.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.
}
于 2013-06-18T10:08:44.413 回答