[eluser]Unknown[/eluser]
I have used CI for a few projects (very successfully!) and have run into this issue several times. I can never seem to find a definitive answer on it, so thought I would ask.
Is it acceptable to define functions in views? The response usually is no, they should be in a helper. However, I find situations when doing complicated formatting in a view that it makes sense to take a chunk of code out of a loop and put it in a function for organization sake. This code will never be reused outside of the view, so putting it in a helper doesn't really make sense.
The main issue I run into with this is that controller variables are not accessible in view functions. I get around this by using globals, but I have a feeling that is not advisable. I would be interested in opinions on this.
in controller:
Code:
$this->load->view('myview', array('mydata'=>$data));
in view:
Code:
GLOBAL $_mydata;
$_mydata = $mydata;
do_something_complicated();
function do_something_complicated()() {
GLOBAL $_mydata;
//do something with mydata
}
So...are functions in views ok?...and what is the best strategy for getting at controller vars?