views:

390

answers:

6

I need to have a place to put some common functions that various view scripts will use such as creating some html by passing it a variable. I know about using helpers, but I want to be able to put many functions inside it not just one helper for each function. Is it a plugin that I need to create?

thanks

A: 

Sounds like what you want is the partial helper

If you don't want to use helpers (including the partial helper) you might as well just create some global functions, stick them in some file, and include it from your bootstrap file.

timdev
No, he should not use partial helper :)
Tomáš Fejfar
+3  A: 

Hm, 'sounds a bit smelly'. What kind of functions would these be? If your design is ok, you shouldn't have a need for this kind of dustbin class. If it is really all about view then you should create view helpers, view partials or partial loops!

tharkun
It certainly does sound a bit smelly. Sounds like he just wants to be lazy, so he might as well just define some global functions.
timdev
This is the right way. Use view helpers to include some common functions to view ;)
Tomáš Fejfar
+4  A: 

A view helper is definitively the way to go. You can group a collection of similar or related functions using a simple design pattern for your view helper:

class App_View_Helper_Example extends Zend_View_Helper_Abstract
{
    /**
     * @param  mixed|null $var
     * @return App_View_Helper_Example 
     */
    public function example($var = null)
    {
        if ($var === null) {
            return $this;
        }
        return $this->function1($var); // shortcut to method most used
    }

    public function function1($var)
    {
        return $this->view->escape($var);
    }

    public function function2($var1, $var2)
    {
        return $this->view->escape(sprintf('%s: %d', $var1, $var2));
    }

    // and so on... 
}

This allows you to call your helper methods in your view like this:

$this->example($var);
$this->example()->function1($var);
$this->example()->function2($var1, $var2);

I used this approach for a Google Static Map helper which provides a centered()-method to display a map centered at a given location and a byMarkers()-method that displays a static map automatically centered and zoomed around a list of given markers.

The only problem you may encounter is keeping a state in your helper across different view scripts (e.g. when using layouts or partials) as the helper will be reconstructed with every single view script. To store state across these boundaries you'll have to resort to Zend_Registry or some static member field.

Stefan Gehrig
a static member field to store the state of an instance should be avoided imho. I would maybe build a registry helper. In that case Zend_View will ensures that the registry is a singleton within the context of the View. I am still very curious about other ideas, the strange thing is: Zend Framework doesn't aknowledge these use cases.
Exception e
A: 

If you don't want a 'bunch of helpers' (which isnt really all that bad, as other posters have suggested), you can extend Zend_View, add the member methods, then set the Viewrenderer to your extended View.

http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer

Justin
A: 

Thank you all for the suggestions. I discovered that you can use a view helper (like Stefan said) to store more functions by just returning $this from it like :

class Zend_View_Helper_FormVars
{


    public function formVars(){

     return $this;

    }

    public function openFormFieldGroup($name=''){

     $html='';
     $html.='<div id="formFldGrpWrapper">';
     $html.='<div id="formFldGrpName"><b>'.$name.'</b></div>';
     return $html;

    }

}

Now in my view script I can use it like this:

$formVars=$this->formVars();
$html.=$formVars->openFormFieldGroup('General');

But I'm also interested in what Justin stated that I can have a common extended view helper? That all my views or controllers can access for doing repetative tasks like some html divs/styles, etc.... How would I go about getting that set up?

thanks.

EricP
see my next answer. A comment is too small to explain this.
Exception e
A: 

But I'm also interested in what Justin stated that I can have a common extended view helper? That all my views or controllers can access for doing repetative tasks like some html divs/styles, etc.... How would I go about getting that set up?

In the answers you ask this additional question. My answer deals with that too.
First you need to ask yourselves why you want to have multiple helper functions in one class

One reason is that you saves you extra classes and file includes. How could you do so?

If they are related you can put them into one view helper. But don't do things like

$this->htmlWrapper()->wrapParapgraph()->wrapContentBox()
    ->translateFromTo('NL', 'EN');

translateFromTo(…) has nothing to with html-wrapping.

If you want to optimize your includes, you can put you common helper code into a derived View-class:

class MyView extends Zend_View
{
    function wrapParagraph() {}
    function otherFunction() {}
}

This option is also mentioned in the zend framework guide as a means of optimization.

Please note that view helper reusability isn't affected by the choice to create view helpers as separate classes. You automatically get access to the current view oject if your helper extends Zend_View_Helper_Abstract.

class My_View_Helper extends Zend_View_Helper_Abstract
{
    function wrapParagraph($content) {
        // do something …
        return $this->someOtherViewHelper();
    }

}

Further you wrote

$formVars=$this->formVars();

This doesn't make sense actualy, since Zend_View registers only one view helper per view isntance.

$formVars=$this->formVars();
$formVars->doOneThing();
$formVars->doSecondThing();

is equivalent to

$this->formVars()->doOneThing();
$this->formVars()->doSecondThing();

The Singleton aspect has a severe impact on the way you design view helpers as you see.

Exception e