Skip to content


Calling a View Helper from inside another View Helper in Zend Framework

Update 8th April 2009: Apparently as of 1.7.6 $this->view->[helperName()] now works with no extra work required so the info in this post is only necessary if you’re using <= 1.7.5. Thanks to Mike van Lammeren for the heads up,  see Mike’s comment below.

If you’re relatively new to Zend Framework, some of the little details can stump you. To call a view helper from within another view helper, you need to give the helper some, er, help. Helpers will try to call a function called setView to set up $_view, so just add it:

Add these lines to the view helper:

private $_view;
public function setView($view) {
$this->_view = $view;
}

then to call the View Helper you need, use $this->_view->anyHelperYouLike();

Edit: I found this information somewhere on the web, but can’t remember the source. Any information appreciated!

Share:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google
  • Furl
  • Reddit
  • StumbleUpon
  • Technorati
  • description
  • Ma.gnolia
  • Slashdot
  • Spurl
  • Tumblr
  • TwitThis

Posted in zend framework. Tagged with .


5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Hi Joe!

    Thanks to your post, I found the answer to this problem. However, I suspect that your information is a little out of date.

    In v1.7.6 of the Zend Framework, the current stable version, the Zend_View_Helper_Abstract class has a public $view variable and public setView() class, so as long as your view helpers have extended Zend_View_Helper_Abstract, then you can access other helpers like this:

    $this->view->anyHelperYouLike();

  2. Joe said

    Thanks Mike, I hadn’t noticed that this had been added. I’ll give it a go next time. I’ll leave this post up in case anyone is using an old version of the Zend Framework…

  3. Bibek said

    The post and the comment both were a help.
    I was getting NULL with var_dump($this->view) since I hadn’t extended Zend_View_Helper_Abstract.

    Cheers

  4. Behrng No said

    Hi this is a link
    http://devzone.zend.com/article/3412

  5. Joe said

    Thanks for that… for some reason I have never found the ZF documentation too useful, but the devzone stuff is great… cheers!

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.