Eduard Luca
Eduard Luca

Reputation: 6612

Pass variable from layout to view in ZF

I'm trying to pass a variable from the master layout to the view, but I'm having no luck.

layout.phtml

$this->hadMessages = true;

myview.phtml

var_dump($this->hadMessages);

The var_dump always comes back with NULL. From what I've read, the layout is a view too, so it should be in the same context, right?

I'm using Zend Framework 1.11.

Upvotes: 1

Views: 120

Answers (2)

zelibobla
zelibobla

Reputation: 1508

There is no way to pass variable from layout to view, because according to MVC pattern you shouldn't have tasks like this. Wether layout contains some messages or not should be decided on controller or bootstrap level, not in layout itself. It means in controller you should make all needed assignments like $this->view->layout_messages_shown = true and get this variable value both in layout and view like echo ( $this->layout_messages_shown ? "messages shown" : "messages hidden" )

Upvotes: 0

Tim Fountain
Tim Fountain

Reputation: 33148

The layout is rendered after the view, so that's why this doesn't work. Depending on what you are trying to do you might be able to achieve the desired effect with the help of a controller plugin.

Upvotes: 3

Related Questions