JamesBarnsley
(Jamesbarnsleyfreelance)
December 15, 2010, 11:06pm
1
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?
JamesBarnsley
(Jamesbarnsleyfreelance)
December 16, 2010, 12:43am
2
JamesBarnsley:
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.
rymonator
(Ryan Bobrowski)
December 16, 2010, 1:08am
3
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.
mdomba
(Maurizio Domba Cerin)
December 16, 2010, 8:09am
4
Take a read at this wiki article -Use shortcut functions to reduce typing
mikl
(Mike)
December 16, 2010, 8:42am
5
… 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.
danaluther
(Dana Luther)
December 16, 2010, 2:55pm
6
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.)