0

I have some controller Index. There I defined variable:

class IndexController extends Zend_Controller_Action
{
      function IndexController()
      {
           $this->view->some_val = 100;
      }
}

And the layout is like this:

<html>
<p><?= $this->some_val; ?></p>
<?= $this->getLayout()->content; ?>
</html>

But in th this case I get NULL instead of 100. I tried to define it in preDispatch function but result is the same. Could anybody help pls? Thanks to all in advance

Anthony
  • 3,218
  • 3
  • 43
  • 73

2 Answers2

2

If you need to save a reusable variable, use the placeholder view helper

public function indexAction() {
    $this->view->placeholder('some_value')->set(100);
}

and in any view script, or layout

echo $this->placeholder('some_value')->getValue();   // -> 100
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
2

One one would be as @Yanick Rochon wrote. Another way would be to assing variables directly to your layout(), e.g.

class IndexController extends Zend_Controller_Action
{
      function IndexController()
      {
           $this->view->layout()->some_val = 100;
      }
}

Then in your layout;

<p><?= $this->layout()->some_val; ?></p>
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Could you be so kind to explain what is the difference between assigning variables to the View instance and to the the Layout? Here's my question here about it http://stackoverflow.com/questions/11396521/what-is-layout-and-what-is-view-in-zf-when-and-whose-variables-should-i-use-and . Kindly ask you to explain. Thank you. – Green Jul 09 '12 at 14:58