Zend Framework 2-布局和变量 [英] Zend Framework 2 - Layout and variable
本文介绍了Zend Framework 2-布局和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的所有视图都使用了一个布局,我需要从控制器中为该布局分配一个变量,如果我在控制器上使用此方法将不起作用:
i have a layout used by all my views and i need to assign a variable from a controller to this layout , if i use this method on a controller it doesn't work :
public function indexAction()
{
return new ViewModel( array(
'testvar' => 'bla',
));
}
有人可以帮助我吗?
谢谢
推荐答案
Rob Allen has posted a great article about how to access view variables in another view model (e.g.: layout)
基本上,以下代码放在您的layout.phtml中,可以满足您的需求:
Basically the following code, placed inside your layout.phtml, will match your needs:
<?php
$children = $this->viewModel()->getCurrent()->getChildren();
$child = $children[0];
?>
<!-- some HTML -->
<?php echo $this->escape($child->myvar);?>
这篇关于Zend Framework 2-布局和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文