Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend view helper with multiple methods?

class My_View_Helper_Gender extends Zend_View_Helper_Abstract
{
  public function Gender()
  {
    //
  }
}

"The class method (Gender()) must be named identically to the concliding part 
 of your class name(Gender).Likewise,the helper's file name must be named 
 identically to the method,and include the .php extension(Gender.php)"
 (Easyphp websites J.Gilmore)

My question is: Can A view Helper contain more than one method?And can I call other view helpers from within my helper?

thanks

Luca

like image 578
luca Avatar asked May 13 '11 12:05

luca


1 Answers

Yes, helpers can contain additional methods. To call them, you have to get hold of the helper instance. This can be achieved either by getting a helper instance in the View

$genderHelper = $this->getHelper('Gender');
echo $genderHelper->otherMethod();

or by having the helper return itself from the main helper method:

class My_View_Helper_Gender extends Zend_View_Helper_Abstract
{
  public function Gender()
  {
    return $this;
  }
  // … more code
}

and then call $this->gender()->otherMethod()

Because View Helpers contain a reference to the View Object, you can call any available View Helpers from within a View Helper as well, e.g.

 public function Gender()
 {
     echo $this->view->translate('gender');
     // … more code
 }
like image 66
Gordon Avatar answered Nov 03 '22 00:11

Gordon