Helper methods for views

We have our Controller class which extends the CController class for our helper methods for controllers.

How would the same thing be done for the views? Where do we put helper methods for these.

Alternatively is there any sort of global application helper method file etc?

Any ideas folks? I really need your help on this one.

I’m a Yii noob but I think you’re supposed to use widgets. For example, create a file called NewWidget.php in /protected/components and define it as:


class MyWidget extends CWidget

{

    public function run()

    {

        //yadda

    }

}

And then in the view you can run the widget using:


$this->widget('application.components.MyWidget');

Working for me nicely so far.

Take a read at this wiki article -Use shortcut functions to reduce typing

… and maybe check the source of framework/web/helpers/CHtml.php. It’s a collection of static helper methods, bundled in a nice class. It could make sense to do the same, if you have a lot of somehow related helper methods.

There’s also a handy little extension called HGrammar which is a good example of collecting various related functions for use in a view (lets you pluralize words, correctly add a or an, etc.)