2013-06-18 138 views
1

我正在尝试使用zf2创建嵌套视图模型。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.

任何想法我做错了什么?

+0

你试图呈现'about' layout.phtml'里面?但你将它附加到'index.phtml'。 – claustrofob

回答

1

您没有正确使用它。

layout.phtml

$ aboutView将只分配给视图模型命名为$视图作为一个孩子。要访问此,您将需要index.phtml使用

index.phtml

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

如果你想分配一个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. 
} 
+0

谢谢。为你的答案它确实工作。我的目标是为布局分配新变量。 – Haver

+1

public function indexAction() { $ layout = $ this-> layout(); //关于 $ aboutView = new ViewModel(); $ aboutView-> setTemplate('application/index/about'); $ layout-> addChild($ aboutView,'about'); $ view = new ViewModel(); return $ view; } – Haver