Current Zend View Path Helper

by Gabi Solomon

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. Which is and isnt 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 change those path i gave in the view file.

This is why i wrote a small View_Helper to return the current view folder.
Unfortunately this will not work on a Zend framework out of the box, it will require some modifications to Zend_View_Abstract.

I have placed the code on ZF Snippets website, a project that i really like, and i would ask you to place any comments and suggestions for this helper on that webpage.

[ZF Snippets code]


  • http://weierophinney.net/matthew/ Matthew Weier O’Phinney

    A couple notes on this. First, using the full path relative to a view script directory is a better practice, as it encourages you to consider the relative domain of the view scripts you are consuming. If you want to shortcut this, add the current directory to your view script paths from within your view script or your controller:
    $this->addScriptPath(dirname(__FILE__)); // within view script

    Second, don’t modify Zend_View_Abstract. Use proper OOP practices and extend Zend_View instead. ;)

  • http://www.gsdesign.ro/ Gabi Solomon

    The way i wanted to do it is to have the base path as a full path ( /home/user/applications/view/scripts ) and in my views i render scripts by givig them the path within that path … /dir1/dir2/script.phtml and as i saw in the view abstract this gets concatenated and you will get the full path.

    I might be wrong but having just a few ScriptPaths will make the view have fewer checks to find the full path.

    As for the extending part, you are absolutely correct, but i didnt knew how to extend the View and also tell the Framework to use this view instead of the Original Zend_View.

  • http://weierophinney.net/matthew/ Matthew Weier O’Phinney

    Basically every component of ZF’s MVC may be replaced. In the case of the view, the easiest way is to inject it into the ViewRenderer in your bootstrap:

    $view = new My_View();
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(‘ViewRenderer’);

    Once you’ve done that, you’re good to go.

  • http://www.gsdesign.ro/ Gabi Solomon

    Thanks for the tip,
    I knew that you can extend them and use them afterward but didnt new how to replace the View in all the places that its used especially since i use Zend_Layout with MVC enebled.