문제

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

도움이 되었습니까?

해결책

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);?>

다른 팁

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');
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top