Zend Framework is a generic file for hosting functions that can be accessed from a view

I need to have a place to put some common functionality that different view scripts will use, like generating some html by passing a variable to it. I know about using helpers, but I want a lot of functionality built into it, not just one helper per function. Is this the plugin I need to create?

thank

+2


source to share


6 answers


View Assistant is definitely the way to go. You can group a collection of similar or related functions using a simple template 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 the Google Static Map helper, which provides a method centered()

to display the map centered at a given location and a byMarkers()

-method that displays a static map automatically centered and scaled around a list of given markers.

The only problem you may run into is keeping the state in your helper across different view scenarios (e.g. when using layouts or partial), as the helper will be reverse engineered with every single script view. To keep state across these boundaries, you need to resort to Zend_Registry

or some kind of static member field.

+4


source


Hm, 'sounds a little smelly.' What functions will they be? If your design is fine, you shouldn't need this dust class. If this is really all about the view, you should create view helpers, view partial or partial loops!



+3


source


It looks like you want a partial helper

If you don't want to use helpers (including partial helpers), you can simply create some global functions, insert them in some file, and include it from your bootstrap file.

0


source


If you don't want a "bunch of helpers" (which really isn't as bad as other posters suggested), you can extend Zend_View, add member methods, and then set the Viewrenderer to an expanded view.

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

0


source


Thanks everyone for the suggestions. I found that you can use a view helper (like Stefan) to store more functions by simply returning $ this from it, like this:

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 opinion the script I can use it like this:

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

      

But I'm also interested in Justin stating that I might have a generic extended view helper? That all my views or controllers can access to do repetitive tasks like some html divs / styles etc. How can I get this setting?

thank.

0


source


But I'm also interested in Justin stating that I might have a generic extended view helper? That all my views or controllers can access to do repetitive tasks like some html divs / styles etc. How can I get this setting?

In the answers, you ask this additional question. My answer also addresses this. First you need to ask yourself why you want to have multiple helper functions in the same class

One of the reasons is that you are saving additional classes and files. How could you do this?

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

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

      

translateFromTo(…)

has nothing to do with html-wrapping.

If you want to optimize your includes, you can wrap the generic helper code in a derived View class:

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

      

This parameter is also mentioned in the zend framework manual as an optimization tool.

Note that the choice to reuse the view helpers is independent of the choice to create view helpers as separate classes. You automatically get access to the current view if your helper expands Zend_View_Helper_Abstract

.

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

}

      

Next, you wrote

$ formVars = $ this-> formVars ();

It doesn't make sense as it is, since it Zend_View

only registers one view helper for each isntance view.

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

      

equivalent to

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

      

The singleton aspect has a strong influence on how you design view helpers, as you can see.

0


source







All Articles