CodeCrack
CodeCrack

Reputation: 5363

Is it possible to get all set view variables in Zend Framework?

If I have a view and want to see all the set variables for the particular view, how would I do it?

Upvotes: 3

Views: 4678

Answers (4)

Jean Carlo Machado
Jean Carlo Machado

Reputation: 1618

There's a more elegant way: $this->viewModel()->getCurrent()->getVariables();

For a nested viewModel: $this->viewModel()->getCurrent()->getChildren()[0]->getVariables();

Upvotes: 7

user2942321
user2942321

Reputation: 29

$this->view->getVars()

or from inside the view

$this->getVars()

Upvotes: 2

axiom82
axiom82

Reputation: 632

In my case, I had a need to load a partial view from within another view. This was very easy, actually. Instead of worrying about getting the variables to pass through from a parent view to a child, simply pass the parent view object.

<?php echo $this->partial('my-view.phtml', $this); ?>

Upvotes: 1

drew010
drew010

Reputation: 69957

Variables assigned to a Zend_View object simply become public properties of the view object.

Here are a couple of ways to get all variables set in a particular view object.

From within a view script:

$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

From a Zend_View object in a controller:

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

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

The last example would work just as well for a view object created manually using $view = new Zend_View();

Upvotes: 9

Related Questions