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
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.
Keep on writing good posts!
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 :)
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. :/
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.
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";
}
why should i do that?
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.
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!
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
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…