understack
understack

Reputation: 11580

Zend Framework: View variable in layout script is always null

I set a view variable in someAction function like this:

$this->view->type = "some type";  

When I access this variable inside layout script like this:

<?php echo $this->type ?>

it prints nothing. What's wrong?

My application.ini settings related to layout

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "layout" ; changed 'default' to 'layout'

Edit

This thread suggests the alternate solution, but looking for solution to above problem. And this was working in Zend 1.6.2. I just upgraded to 1.10 and it stopped working.

Edit

If I set this view var inside any _init Bootstrap function, it works.

Upvotes: 0

Views: 3342

Answers (3)

Christian Burger
Christian Burger

Reputation: 675

I believe the layout view object and the action view object are separate instances of the Zend_View class.

I think this is the correct way to pass variables from the controller to the layout:

/**
 * Controller action
 */    
public function indexAction()
{
    $this->_helper->layout()->assign('myName', 'John Doe');
}

and then in your layout script you can access the variables by referencing the layout object like this:

<html>
<body>
<?php echo $this->layout()->myName; ?>
</body>
</html>

Upvotes: 1

bitfox
bitfox

Reputation: 2292

Do you have the following entry in your application.ini file?

resources.view[] =

So, you can initialize the view with no options and use it through:

<?php echo $this->type ?>

Upvotes: 0

homelessDevOps
homelessDevOps

Reputation: 20726

If you want to assign something to your layout you have to go an other way:

// get the layout instance
$layout = Zend_Layout::getMvcInstance();

// assign fooBar as Name to the layout
$layout->name = 'fooBar';

Upvotes: 7

Related Questions