0

I have a Controller, Layout, Custom view helper. I'm passing a data from controller $this->view->foo = 'foo'; normally I get it on my layout.phtml,here I'm calling a custom view helper $this->navbar(); on layout.

How can I access that foo within my view helper?

<?php
class Zend_View_Helper_Navbar extends Zend_View_Helper_Abstract
{
    public function setView( Zend_View_Interface $view )
    {
        $view = new Zend_View();
        $view->setScriptPath(APPLICATION_PATH . '/views/scripts/partials/');
        $this->_view = $view;
    }

    public function navbar()
    {
            return $this->_view->render('navbar.phtml');
    }

}

this is my view helper

4

2 回答 2

0

更改您的辅助函数,使其接受参数,如下所示:

Zend_View_Helper_Navbar 中:

public function navbar($foo="")
{
        $this->_view->bar = $foo;
        return $this->_view->render('navbar.phtml');
}

然后,在navbar.phtml 中:

<?php echo $this->bar; ?>

这样,传递给辅助函数的任何参数值都将显示在 navbar.phtml 中。之后,您可以照常从控制器文件中传递参数。

在您的控制器文件中:

$this->view->foo = "custom parameter";

在您的视图脚本或 layout.phtml 中,调用传递参数的导航栏助手:

<?php echo $this->navbar($this->foo);?>
于 2013-10-25T16:55:38.660 回答
0

Zend_View_Helper_Navbar 扩展了包含 $view 的 Zend_View_Helper_Abstract。您所要做的就是:

public function navbar()
{
    $this->view->setScriptPath(APPLICATION_PATH . '/views/scripts/partials/');
    $foo = (isset($this->view->foo)) ? $this->view->foo : '';
    // your code using $foo
    return $this->view->render('navbar.phtml');
}
于 2013-10-25T14:10:29.237 回答