CodeIgniter Forums
Data that my 'views' can access which were set in MY_controller, possible? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Data that my 'views' can access which were set in MY_controller, possible? (/showthread.php?tid=34415)



Data that my 'views' can access which were set in MY_controller, possible? - El Forum - 09-29-2010

[eluser]rreynier[/eluser]
Hey guys, is there any way I can make some data set in MY_controller accessible to my views easily?

Right now in MY_controller I am doing something like

Code:
$this->some_variable = $this->some_model->get_some_variable();

Then, in my normal controller, I am loading the view like this:

Code:
$data['some_variable'] = $this->some_variable;
$this->load->view('someview', $data);

Is there any way to add variables directly to my view without having to go about it this way?


Data that my 'views' can access which were set in MY_controller, possible? - El Forum - 09-29-2010

[eluser]Dennis Rasmussen[/eluser]
You can create a variable named data in your MY_controller.

Code:
$this->data = array();
$this->data['some_variable'] = $this->some_model->get_some_variable();
$this->data['page_title'] = 'Welcome!';
// ...

Then in your new controller:

Code:
$this->data['foo'] = 'bar';
$this->load->view('someview', $this->data);

That's probably the easiest way to do it.


Data that my 'views' can access which were set in MY_controller, possible? - El Forum - 09-29-2010

[eluser]rreynier[/eluser]
That kinda makes sense. So instead of adding to the $data array like I always do.. I will just load additional data to $this->data instead of $data. It would still be cool if there was a way to make stuff available to views from MY_controller.