Question

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

Was it helpful?

Solution

Change your helper function such that it accepts a parameter, as shown:

In Zend_View_Helper_Navbar:

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

Then, in navbar.phtml:

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

This way, whatever parameter value passed to the helper function will be displayed in the navbar.phtml. After that, you can pass the parameter from your controller file as usual.

In your controller file:

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

In your view script, or layout.phtml, call the navbar helper passing the parameter:

<?php echo $this->navbar($this->foo);?>

OTHER TIPS

Zend_View_Helper_Navbar extends Zend_View_Helper_Abstract which contains $view. All you have to do is :

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');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top