分配给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
Run Code Online (Sandbox Code Playgroud)
从Zend_View控制器中的对象:
$this->view->foo = 'test';
$this->view->bar = '1234';
$viewVars = get_object_vars($this->view);
// $viewVars now contains all public properties (view variables)
Run Code Online (Sandbox Code Playgroud)
最后一个示例对于手动创建的视图对象也同样有效 $view = new Zend_View();
有一种更优雅的方式: $this->viewModel()->getCurrent()->getVariables();
对于嵌套的viewModel: $this->viewModel()->getCurrent()->getChildren()[0]->getVariables();