Zend Framework: Render If Exists

By , Sunday 12th December 2010 4:19 pm

Summary

This is a quick post to discuss the rather simple view helper I created for rendering a Zend Framework style view file only if it exists. Generally asking the code to render a file which doesn’t exist will throw an exception. Therefore I created a wrapper for the Zend_View::render() method which determined whether the file exists and if so renders, otherwise simply returns an empty string.

Joost J. Bakker IJmuiden - http://www.flickr.com/photos/joost-ijmuiden/

Motivation

The motivation for this is that I have one controller/view/etc which shows slightly different output depending on a parameter. Some of these outputs have a related help information box defined, some do not. I didn’t want to have empty files hanging around my code just as much as I didn’t want to have try {} catch {} blocks in my view file. Therefore, I quickly created a ‘render if exists’ view helper for use in my code.

I realise that here I could have just wrapped the render method within a try{}catch{} within the view helper itself. In this case however any exceptions thrown from the rendered view would not bubble back up in the case of something going wrong.

How It Works

The view helper is created by extending Zend_View_Helper_Abstract as normally, this injected with the view upon use. The view helper is passed the view file you wish to render:

$this->renderIfExists('info-box.phtml');

Internally the object gathers the script paths from the view object and loops over them trying to find the file:

    /**
     * Check to see if a view script exists
     *
     * @return boolean
     */
    protected function _fileExists()
    {
        $paths = $this->view->getScriptPaths();
        foreach ($paths as $path) {
            if (file_exists($path . $this->_file)) {
                return true;
            }
        }
        return false;
    }

If a matching file is found it uses the standard Zend_View::render() method, otherwise it returns an empty string.

Summary

This is a very simple view helper to render a view file if it exists and simply return an empty string if not. It does not use a try{} catch{} block which would prevent any deeper exceptions being thrown from bubbling up and making themselves known. Instead it loops through the script paths defined within the view object and attempts to detect the file before trying to render it.

The full file can be found at my github.com/lloydwatkin/Demos/blob/master/zendframework/renderifexists/RenderIfExists.php” target=”_blank”>github repo.

Liked this post? Follow this blog to get more. 

5 Responses to “Zend Framework: Render If Exists”

  1. Tim says:

    Thanks for this article.

    How can I implement this into Zend? I can’t get it to work. I placed the file in my /library directory but I am keep getting an error:
    Message: Method “renderIfExists” does not exist and was not trapped in __call()

    What do I need to do to finally get it to work?

    Thanks in advance!

    • Providing you’ve kept the same name then you’ll need to tell Zend_View the additional view helper path, e.g.

      $view = new Zend_View();
      $view->addHelperPath(‘Path/To/Helper’, ‘Pro_View_Helper’);

      If you are using full ZF MVC then you can add additional lines of config to your application.ini:

      resources.view.helperPath.Pro_View_Helper = “Path/To/Helpers”

      • Tim says:

        Thanks!

        However I am not getting any further. I am still having the same problem:

        $view = new Zend_View(); $mypath=$this->view->getHelperPaths(); $view->addHelperPath($mypath[‘Login_View_Helper_’][0], ‘RenderIfExists’); $this->renderIfExists(‘login.phtml’);

        Above code gives me the error:
        Message: Method “renderIfExists” does not exist and was not trapped in __call().

        My path to my helper is correct. I’ve checked it with echo $mypath[‘Login_View_Helper_’][0]

        Any ideas where to look for a solution?

Leave a Reply

You must be logged in to post a comment.

Panorama Theme by Themocracy

1 visitors online now
0 guests, 1 bots, 0 members
Max visitors today: 16 at 08:14 am UTC
This month: 17 at 19-09-2017 09:35 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC