user3821280
user3821280

Reputation: 39

How to send values to views?

I'm trying to send some values to the view (layout + partials) using this code (all of these variables have 1 as value).

$this->layout()->setVariables(array(
    'nbTotalLignes'     => $nbTotalLignes,
    'nbTotalPages'      => $nbTotalPages,
    'comptes'           => $comptes,
    'numPageCourante'   => $numPageCourante,
    'nbComptesAffiches' => $nbComptesAffiches,
    'comptesAffiches'   => $comptesAffiches,
));

But when I try to display these in the view using this code echo (isset($this->nbTotalLignes)?1:0, I get 0. How do I please to fix that ?

Upvotes: 0

Views: 36

Answers (1)

Exlord
Exlord

Reputation: 5371

each view has its own scope. $this->layout()->setVariables set the variables for the layout and not your current actions view.

to send vars to the current actions view you have 2 options :

return array('var1'=>1);

zend will automatically convert this to a viewmodel. or

$view = new ViewModel();
$view->setVariables(array('var1'=>1))
return $view;

with this approach you can also set a different template

$view->setTemplate('a different template path'); 

and choose not to render layout

$view->setTerminal(true)

Upvotes: 1

Related Questions