Determining if a ZF view helper exists

This is another one of those posts that exists as a record for me so I can find it again if i need it!

If you need to know whether a view helper exists before you call it, one way is to write a simple view helper to tell you:


class App_View_Helper_HelperExists extends Zend_View_Helper_Abstract
{
function helperExists($name) {
return (bool)$this->view->getPluginLoader('helper')->load($name, false);
}
}

You can then use it in a view scripts like this:


< ?php if ($this->helperExists('doMagic')) { echo $this->doMagic(); } ?>

As Jeremy mentions in the comments, this code came out of a discussion on the PHPNW IRC channel #phpnw

11 Responses to “Determining if a ZF view helper exists”

  1. 1 Matthew Weier O'Phinney

    You can also simply use getHelper() on the view object, which will return a boolean false if not found, or the helper object itself if found.

  2. 2 umpirsky

    Keep on writing good posts!

  3. 3 phpcodemonkey

    Mathew, we tried that and it throws an exception, hence this code was discussed and worked out in #phpnw IRC following my initial question. Thanks @akrabat :)

  4. 4 Bonaparte

    Never had to check whether a view helper exists.

    I'm wondering why would anyone want to check if a view helper exists and conditionally call the view helper. :/

  5. 5 chancegarcia

    phpcodemonkey: if it throws an exception, that says to me the helper execution should be in a try-catch and handled that way.

    Bonaparte makes a good point, why do you need to check for a helpers existence then conditionally call it. The way I see it, if I'm calling a helper, then I need it to exist. If it doesn't exist, that's an error condition that should be handled. Besides, if the helperExists helper doesn't exist, you're back at square one.

  6. 6 chancegarcia

    If it throws an exception, shouldn't you just use a try-catch block? Seems to me that this solution may run into an issue if the helperExists() helper doesn't exist.

    Assuming $this is the view object:

    try {
    $this->foo();
    } catch (Zend_Loader_PluginLoader_Exception $zlpe){
    echo "oh noes foo helper is missing";
    }

  7. 7 gregor

    why should i do that?

  8. 8 Josh Ribakoff

    Cool but don't see the use either. Just try and use it. The exception itself is enough to tell you it doesn't exist. It also nicely prints out the helper paths that it looked in, if you noticed.

  9. 9 iSac

    I actually have a use for it. I have a Mootools library where setOption($key, $value) checks if $key ends in 'Options' and $value is an array. If so it looks to see if there is a widget with the name $key: if so it calls setOptions($value) on it; if not it sets the option as usual.

    i.e.
    $ajaxHelper = $this->ajaxSubmission();
    $ajaxHelper->setOption('waiterOptions', array('message' => 'Please Wait', 'color' => 'blue', 'et' => 'cetera'));

    The second line will call the setOptions method of the Waiter helper if it exists, and if not just encodes it as usual. If it does exist this allows the "target widget" (waiter in this case) to do anything special it needs to with a particular waiter option.

    Thanks Rob!

  10. 10 Slavi

    is there a way to make the a catch-all view helper that handles calls to non-existent view helpers ?
    maybe replace it with HTML comments if not found.

    e.g.

    Maybe Rob was checking for view helper existence is because he's dynamically generating the helper names ?
    Am I right Rob ?

    Slavi

  11. 11 Rob...

    Slavi,

    I suspect that you'd need to extend Zend_View to do that.

    Dynamically generated view helpers is a good use-case :) In reality though, I was chatting with Jeremy on IRC and helped him solved his problem :)

    Regards,

    Rob...

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.