Proto
Proto

Reputation: 247

Zend check if view exist - class does not work

I have found this class:

https://github.com/lloydwatkin/Demos/blob/master/zendframework/renderifexists/RenderIfExists.php

The code looks like this:

    <?php
/**
* View helper to render a view file if it exists
*
* @author Lloyd Watkin
* @since 12/12/2010
* @package Pro
* @subpackage View
*/

/**
* View helper to render a view file if it exists
*
* @author Lloyd Watkin
* @since 12/12/2010
* @package Pro
* @subpackage View
*/
class Pro_View_Helper_RenderIfExists
    extends Zend_View_Helper_Abstract
{
    /**
* Errors
*
* @var string
*/
    const INVALID_FILE = 'Invalid file parameter';

    /**
* Holds file name for processing
*
* @var string
*/
    protected $_file;

    /**
* Takes a products options array and converts to a formatted string
*
* @param string $file
* @return string
*/
    public function renderIfExists($file)
    {
        if (!is_string($file) || empty($file)) {
            throw new Zend_View_Exception(self::INVALID_FILE);
        }
        $this->_file = $file;
        if (false === $this->_fileExists()) {
            return '';
        }
        return $this->view->render($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;
    }
}

I want to check if a view exists; If it does: Show it.

I have placed the file in my /library/. When I call $this->renderIfExists('info-box.phtml'); from the controller I get this error:

Message: Method "renderIfExists" does not exist and was not trapped in __call() 

How can I fix this? Thanks in advance!

Upvotes: 0

Views: 1790

Answers (2)

Lloyd Watkin
Lloyd Watkin

Reputation: 505

bit late to this, but the code you've got above is something I wrote. Have you registered the helper path, if w would the framework know where to load the file?

Upvotes: 0

Pieter
Pieter

Reputation: 1774

to call a view helper from the controller, you should use:

$this->view->renderIfExists()

Upvotes: 1

Related Questions