如何在不使用 pass-through 代码的情况下将 View Helper 变量传播到 ZF2 中的远程视图 child?
How can I propagate a View Helper variable to a distant View child in ZF2, without using pass-through code?
我没有完整的 ZF2 堆栈,但我操纵了我的 mostly-non-ZF2 代码以接受 $this->partial()
和 ViewModel()
方法。
我经常发现我有一个 View Helper Partials 树的情况,其中一些 distant-from-root child 需要一个变量 someVar
.
我管理那个变量,通过每个 partial
从根到 child,即使 in-route 部分不需要它。
有没有办法不用管理变量?
例子
//controller.php
echo $this->partial('root.phtml', array('someVar' => $someVar));
//root.phtml
<?
//this variable-pass-through-only step is one I would like to eliminate.
//aka. here someVar is not itself used in root.phtml
//it is only passed onto the child view partial
//I want to eliminate this pass-through-only code.
echo $this->partial('child.phtml', array('someVar' => $this->someVar)):
?>
//child.phtml - leaf child
<?
//variable is actually used for display purpose
echo $this->someVar;
?>
我愿意接受使用非partial
结构的答案,即ViewModel
等
注意:当我删除 pass-through 代码时,希望变量有某种全局范围,但事实并非如此 - 变量不会传递到 child 叶视图部分.我希望 ZF2 中有某种更好的方法来实现我想做的事情。
问题的目标/精神
明确地说,我正在寻找一种方法,使一些变量成为 "global" 变量,它从 partial
/view
的根扩展到叶 .phtml
没有 pass-through 代码,或者可能完全不同的方法我不需要这样做,但不会用 pass-through vars
弄乱我的代码
您可以使用嵌套的 ViewModel
实例来复制局部视图助手的功能。通过独立创建对象,无需将所有变量传递给每个对象。
一个简单的例子。
$main = new ViewModel(['var1' => 'xyz', 'var2' => 'xyz']);
$main->setTemplate('main.phtml');
$foo = new ViewModel(['baz' => 'bob']);
$foo->setTemplate('foo.phtml');
$bar = new ViewModel(['test' => 123]);
$bar->setTemplate('bar.phtml');
// foo.phtml should echo $this->barResultHtml
$foo->addChild($bar, 'barResultHtml');
// main.phtml should echo $this->fooResultHtml
$main->addChild($foo, 'fooResultHtml');
// You will need to set this up to render the view model.
$view = new Zend\View\View();
$view->setRenderer(...);
echo $view->render($main);
我没有完整的 ZF2 堆栈,但我操纵了我的 mostly-non-ZF2 代码以接受 $this->partial()
和 ViewModel()
方法。
我经常发现我有一个 View Helper Partials 树的情况,其中一些 distant-from-root child 需要一个变量 someVar
.
我管理那个变量,通过每个 partial
从根到 child,即使 in-route 部分不需要它。
有没有办法不用管理变量?
例子
//controller.php
echo $this->partial('root.phtml', array('someVar' => $someVar));
//root.phtml
<?
//this variable-pass-through-only step is one I would like to eliminate.
//aka. here someVar is not itself used in root.phtml
//it is only passed onto the child view partial
//I want to eliminate this pass-through-only code.
echo $this->partial('child.phtml', array('someVar' => $this->someVar)):
?>
//child.phtml - leaf child
<?
//variable is actually used for display purpose
echo $this->someVar;
?>
我愿意接受使用非partial
结构的答案,即ViewModel
等
注意:当我删除 pass-through 代码时,希望变量有某种全局范围,但事实并非如此 - 变量不会传递到 child 叶视图部分.我希望 ZF2 中有某种更好的方法来实现我想做的事情。
问题的目标/精神
明确地说,我正在寻找一种方法,使一些变量成为 "global" 变量,它从 partial
/view
的根扩展到叶 .phtml
没有 pass-through 代码,或者可能完全不同的方法我不需要这样做,但不会用 pass-through vars
您可以使用嵌套的 ViewModel
实例来复制局部视图助手的功能。通过独立创建对象,无需将所有变量传递给每个对象。
一个简单的例子。
$main = new ViewModel(['var1' => 'xyz', 'var2' => 'xyz']);
$main->setTemplate('main.phtml');
$foo = new ViewModel(['baz' => 'bob']);
$foo->setTemplate('foo.phtml');
$bar = new ViewModel(['test' => 123]);
$bar->setTemplate('bar.phtml');
// foo.phtml should echo $this->barResultHtml
$foo->addChild($bar, 'barResultHtml');
// main.phtml should echo $this->fooResultHtml
$main->addChild($foo, 'fooResultHtml');
// You will need to set this up to render the view model.
$view = new Zend\View\View();
$view->setRenderer(...);
echo $view->render($main);