2013-06-18 2 views
1

Я пытаюсь создать вложенную модель представления с zf2.zf2: Nesting Просмотр моделей выпуска

Я использую стандарт 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 будут назначены только 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. 
} 
+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

Смежные вопросы