Juc*_*uck 24 layout zend-framework2
我有一个我的所有视图使用的布局,我需要从控制器分配一个变量到这个布局,如果我在控制器上使用此方法它不起作用:
public function indexAction()
{
return new ViewModel( array(
'testvar' => 'bla',
));
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
谢谢
Jos*_*ius 66
在ZF2中有三种方法可以实现这一点(在您的控制器中):
第一:
$this->layout()->someVariableName = 'Some value for the variable';
Run Code Online (Sandbox Code Playgroud)
第二:
$this->layout()->setVariable('someVariableName', 'Some value for the variable');
Run Code Online (Sandbox Code Playgroud)
第三:
$this->layout()->setVariables(array(
'someVariableName' => 'Some value for the variable',
'anotherVariable' => 'Some value for another variable',
);
Run Code Online (Sandbox Code Playgroud)
Sam*_*Sam 39
Rob Allen发表了一篇很棒的文章,介绍了如何在另一个视图模型中访问视图变量(例如:layout)
基本上,以下代码放在layout.phtml中,将满足您的需求:
<?php
$children = $this->viewModel()->getCurrent()->getChildren();
$child = $children[0];
?>
<!-- some HTML -->
<?php echo $this->escape($child->myvar);?>
Run Code Online (Sandbox Code Playgroud)
你有没有尝试过:
$this->layout()->testvar = 'bla';
Run Code Online (Sandbox Code Playgroud)
使用layout控制器插件,您可以检索使用的ViewModel对象layout.phtml.
| 归档时间: |
|
| 查看次数: |
33257 次 |
| 最近记录: |