ZF:布局的视图变量定义


ZF: view variable definition for layout

我有一些控制器索引。我在那里定义了变量:

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

布局是这样的:

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

但在这种情况下,我得到的是NULL,而不是100。我试图在preDispatch函数中定义它,但结果是一样的。有人能帮忙吗?感谢所有提前

@Yanick Rochon写道。另一种方法是将变量直接分配到布局中(),例如

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

然后在你的布局中;

<p><?= $this->layout()->some_val; ?></p>

如果需要保存可重用变量,请使用placeholder视图助手

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

以及在任何视图脚本或布局中

echo $this->placeholder('some_value')->getValue();   // -> 100