Results 1 to 8 of 8

Thread: custom view helpers

  1. #1
    freenity is offline Junior Member
    Join Date
    Apr 2008
    Posts
    8

    Default custom view helpers

    Hi
    I made my custom view helper andto use them I have to add my path to the view object:


    [PHP]$view->addHelperPath("../application/default/views/helpers"); [/PHP]


    But, the problem is that I have to do this in every action where I want to use my helper, and I can't find a way to set this path once, say in a bootstrap....

    Is there any easy and fun ways of doing this? and not having to add the path in every action..

    Thanks.

  2. #2
    Davidoff is offline Junior Member
    Join Date
    Oct 2007
    Posts
    28

    Default

    If you're using the ViewRenderer it couldn't be simpler:

    [PHP]$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelp er('viewRenderer');
    $viewRenderer->initView();

    //your line
    $viewRenderer->view->addHelperPath("../application/default/views/helpers"); [/PHP]

  3. #3
    Elemental's Avatar
    Elemental is offline Senior Member
    Join Date
    Jul 2007
    Posts
    122

    Default

    Davidoff's solution is indeed simple, however it appears as though you're using a conventional modular file structure. If so, then once you have helpers in modules other than default Davidoff's solution will stop working for you.

    You could set the helper path per request via an Action Helper and set it based on the requested module as so:
    [php]
    <?php

    require_once('Zend/Controller/Plugin/Abstract.php');

    class MyApp_Controller_Plugin_ModularHelperPath extends Zend_Controller_Plugin_Abstract
    {
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
    $moduleName = $request->getModuleName();
    $rootDir = Zend_Registry::get('rootDir');
    set_include_path(get_include_path() .
    PATH_SEPARATOR . $rootDir . '/application/' . $moduleName . '/views/helpers/');
    }
    }
    [/php]

    $rootDir = Zend_Registry::get('rootDir') assumes you have set the rootDir in your bootstrap, otherwise you will need to adjust the script to set the paths appropriately.
    Zend Framework Resources: Zend Webinars | Reference Manual | API Docs | Books | FreeNode: #zftalk
    Getting Started Tutorials: Getting started with ZF | Getting started with Zend Auth

  4. #4
    freenity is offline Junior Member
    Join Date
    Apr 2008
    Posts
    8

    Default

    Quote Originally Posted by Elemental View Post
    Davidoff's solution is indeed simple, however it appears as though you're using a conventional modular file structure. If so, then once you have helpers in modules other than default Davidoff's solution will stop working for you.

    You could set the helper path per request via an Action Helper and set it based on the requested module as so:
    [php]
    <?php

    require_once('Zend/Controller/Plugin/Abstract.php');

    class MyApp_Controller_Plugin_ModularHelperPath extends Zend_Controller_Plugin_Abstract
    {
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
    $moduleName = $request->getModuleName();
    $rootDir = Zend_Registry::get('rootDir');
    set_include_path(get_include_path() .
    PATH_SEPARATOR . $rootDir . '/application/' . $moduleName . '/views/helpers/');
    }
    }
    [/php]

    $rootDir = Zend_Registry::get('rootDir') assumes you have set the rootDir in your bootstrap, otherwise you will need to adjust the script to set the paths appropriately.
    Thanks for the reply guys.
    I will try the Davidoffs solution, I was hoping was something that would work by putting it in the bootstrap file once, seems like this should work.

  5. #5
    Davidoff is offline Junior Member
    Join Date
    Oct 2007
    Posts
    28

    Default

    Yep - I haven't worked with modules but I think Elemental has it bang on.

    I guess you could access the request in the bootstrap and make a decision there, but you might as well use a Controller Plugin.

  6. #6
    freenity is offline Junior Member
    Join Date
    Apr 2008
    Posts
    8

    Default

    Hi
    Davidoff's solution didn't work,
    and for the plugin I have a question: why do you include the path instead of using addHelperPath() ?
    I tried including the path in the bootstrap file, but it didn't work.
    Also the path is: ../application/default/views/helpers
    And default it's just a folder that Zend Studio creates (don't know why). But this never changes.
    I don't know if you understood me well, I'm new to ZF so maybe some things I don't quite understood.
    Thanks for the help.

  7. #7
    Davidoff is offline Junior Member
    Join Date
    Oct 2007
    Posts
    28

    Default

    I took the line from your example.

    But you probably need to pass the second parameter with the class name prefix.

    Sample code from my live, working app:

    [PHP]$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelp er('viewRenderer');

    $viewRenderer->initView();

    $viewRenderer->view->addHelperPath(APPLICATION_DIRECTORY . '/views/helpers', 'My_View_Helper');
    [/PHP]

    Example helper class name is class My_View_Helper_ContactSelect for the helper 'contactSelect'

    According to the API Docs, Docs For Class Zend_View_Abstract, the second argument defaults to Zend_View_Helper

  8. #8
    freenity is offline Junior Member
    Join Date
    Apr 2008
    Posts
    8

    Default

    Thanks a lot
    It worked.
    I forgot about the prefix, lol

    Many thanks again Davidoff

Similar Threads

  1. Custom module view helpers
    By Themodem in forum Model-View-Controller (MVC)
    Replies: 1
    Last Post: 07-31-2010, 06:16 PM
  2. Custom View Helpers question,
    By muneeba in forum Model-View-Controller (MVC)
    Replies: 3
    Last Post: 06-14-2010, 02:59 PM
  3. Custom View Helpers not being found
    By ro88o in forum Core Infrastructure
    Replies: 3
    Last Post: 04-22-2010, 07:33 PM
  4. Custom View Helpers
    By dele454 in forum Model-View-Controller (MVC)
    Replies: 7
    Last Post: 07-17-2008, 12:05 AM
  5. MVC and custom helpers
    By sebastian in forum Model-View-Controller (MVC)
    Replies: 2
    Last Post: 04-13-2008, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •