Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend framework 2: How to use view helper in other view helper

Is it possible to use view helper in another view helper? We have to view helpers:

HelpMe1

use Zend\View\Helper\AbstractHelper;

class HelpMe1 extends AbstractHelper
{

    public function __invoke($arg)
    {
        return $arg;
    }
}

HelpMe2

use PathTo\HelpMe1;
use Zend\View\Helper\AbstractHelper;

class HelpMe2 extends AbstractHelper
{

    public function __invoke()
    {
        return '<p>' . new HelpMe1('Text') . '</p>';
    }
}

If this is possible what it the base practice for that?

Regards,

like image 594
Marceli Po Avatar asked Dec 07 '13 13:12

Marceli Po


1 Answers

As long as your helper extends the abstract helper class, the View object is injected into it, and you can access other helpers from there.

class HelpMe2 extends AbstractHelper
{
    public function __invoke()
    {
        return '<p>' . $this->view->helpMe1('Text') . '</p>';
    }
}
like image 87
Tim Fountain Avatar answered Oct 22 '22 00:10

Tim Fountain