Zend Framework Source Code Snippets

Current Script Path

<?php

class GSD_View_Helper_CurrScriptPath extends Zend_View_Helper_Abstract{

    public function currScriptPath($path = false){
        $scriptPath = $this->view->getScriptPaths();
        $filePath = $this->view->getFilePath();

        $viewPath = str_replace($scriptPath, '', dirname($filePath)) . '/';

        if (is_string($path) && !empty ($path)) {
            $viewPath .= $path;
        }

        return $viewPath;
    }

}


abstract class Zend_View_Abstract implements Zend_View_Interface
{
    /**
     * Returns the current file path
     *
     * @return array
     */
    public function getFilePath()
    {
        return $this->_file;
    }
}

Comments

subzero2000 subzero2000
11 months ago

I felt compelled to post after using this. I find it very useful, but came up with what I believe is a better way that avoids modifying Zend_View_Abstract. I create a Local_View class instance instead of a Zend_View instance in my bootstrap. The code for my Local_View class is below:

<?php

class Local_View extends Zend_View
{
private $_current_script_file_name = null;

/**
* Processes a view script and returns the output.
*
* @param string $name The script script name to process.
* @return string The script output.
*/
public function render($name)
{
$this->_current_script_file_name = $this->_script($name);

return parent::render($name);
}

/**
* Returns the current file path
*
* @return array
*/
public function getFilePath()
{
return $this->_current_script_file_name;
}
}

?>

(Hopefully, the code will be readable after I post it.)

subzero2000 subzero2000
11 months ago

I'm going to try posting the code again inside of &lt;pre&gt; tags to see if that makes it more readable. If not, look at the source and convert it from HTML to text, as my formatting is preserved properly there:

<pre>
<?php

class Local_View extends Zend_View
{
private $_current_script_file_name = null;

/**
* Processes a view script and returns the output.
*
* @param string $name The script script name to process.
* @return string The script output.
*/
public function render($name)
{
$this->_current_script_file_name = $this->_script($name);

return parent::render($name);
}

/**
* Returns the current file path
*
* @return array
*/
public function getFilePath()
{
return $this->_current_script_file_name;
}
}

?>
</pre>

subzero2000 subzero2000
11 months ago

Ok, so since formatting using <pre> tags doesn't work in comments, I'm not going to pollute this post with any more comments trying to get it to display properly. It would be nice if when posting comments there was a way to include formatted code which would display properly with formatting preserved in the comment rather than having to post another snippet. Thanks.

davedevelopment davedevelopment
11 months ago

I'd like to allow some sort of markup site wide, but will have to put some effort into sanitising etc.

It's on my list ;)

You must login before commenting on a snippet. If you do not have an account, please register.

Many times when i use Zend_view i have to include view files that are in the same directory, and i am forced to type in the same path over again. Wich is and inst so much work, but during development i often have to place those files in a different folder or rename one of the folders. In witch case i would be forced to chage those path i gave in the view file.

This little helper will return the path to the current view folder.

It also requires adding a new method to Zend_View_Abstract

Created:
gabriel solomon gabriel solomon
11 months ago
Edited:
gabriel solomon gabriel solomon
11 months ago
Revision Id:
85
Edit Message:
Initial Release
Tags:
Zend_View Zend_View _Helper
Comments:
4
Points:
2 (2 votes)

History

r85

Initial Release

gabriel solomon gabriel solomon
11 months ago
diff
r84

Initial Release

gabriel solomon gabriel solomon
11 months ago