Zend Framework, passing variables to view

2020-03-28 17:56发布

问题:

I have a problem with displaying a view. When I pass var to view, view doesn't render.

Controller:

public function indexAction()
{
    $branchModel = new Application_Model_Branches();
    $branches = $branchModel->getAllBranches();
    $this->view->menu = $branches;
}

View (index.phtml):

<h2>Menu</h2>
<?php
    $this->htmlList($this->menu);
?>

When I try debug $branches without assign it to view, all seems to be ok, but when I try push it to view,index.phtml don't appear.

Regards

回答1:

You're just missing an echo in your code, the htmlList view helper returns a value - it doesn't echo it. Some examples of the various form view helpers can be seen here

<h2>Menu</h2>
<?php
    echo $this->htmlList($this->menu);
?>


回答2:

controller

    $this->view->variableName = "Hello World!";//assign here        

    $this->view->assign('variableName1', "Hello new World!");//assign here

view

    echo $this->variableName;//echo here

    echo $this->variableName1;//echo here