3

如果我有一个视图并想查看特定视图的所有设置变量,我该怎么做?

4

4 回答 4

9

分配给Zend_View对象的变量只是成为视图对象的公共属性。

以下是在特定视图对象中设置所有变量的几种方法。

从视图脚本中:

$viewVars = array();

foreach($this as $name => $value) {
    if (substr($name, 0, 1) == '_') continue; // protected or private

    $viewVars[$name] = $value;
}

// $viewVars now contains all view script variables

Zend_View控制器中的对象:

$this->view->foo = 'test';
$this->view->bar = '1234';

$viewVars = get_object_vars($this->view);
// $viewVars now contains all public properties (view variables)

最后一个示例同样适用于使用手动创建的视图对象$view = new Zend_View();

于 2012-09-24T19:08:06.590 回答
7

还有一种更优雅的方式:$this->viewModel()->getCurrent()->getVariables();

对于嵌套视图模型:$this->viewModel()->getCurrent()->getChildren()[0]->getVariables();

于 2014-04-03T01:33:03.470 回答
2
$this->view->getVars()

或从视图内部

$this->getVars()
于 2014-06-06T17:27:17.100 回答
1

就我而言,我需要从另一个视图中加载部分视图。实际上,这很容易。无需担心将变量从父视图传递给子视图,只需传递父视图对象即可。

<?php echo $this->partial('my-view.phtml', $this); ?>
于 2014-05-07T16:51:19.033 回答