CodeIgniter Forums
Pushing data to views with $this->data vs $data? - 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: Pushing data to views with $this->data vs $data? (/showthread.php?tid=42944)



Pushing data to views with $this->data vs $data? - El Forum - 06-24-2011

[eluser]DanielJay[/eluser]
I am working on trying to put some data on almost every page in my project. I am not wanting to copy/paste the same code to almost every function of the entire project. I am considering putting the data that I want to show up on each page in the __construct() of each controller that I would like it to be available on. Then instead of using $data array to push the data to the views I would switch things to $this->data. That way I could access the data I created in the __construct() in each of the sub functions(pages).

What are your thoughts on this? Or is there a better way to get the same database data into my header view?


Pushing data to views with $this->data vs $data? - El Forum - 06-24-2011

[eluser]InsiteFX[/eluser]
MY_Controller


Pushing data to views with $this->data vs $data? - El Forum - 06-24-2011

[eluser]DanielJay[/eluser]
Would you then put the data in $this->data or how would you get that to your views?


Pushing data to views with $this->data vs $data? - El Forum - 06-24-2011

[eluser]iain.b[/eluser]
The HMVC extension is quite useful for this, using Modules::run to return view partials wherever you need them saves a lot of duplicated code.


Pushing data to views with $this->data vs $data? - El Forum - 06-24-2011

[eluser]adityamenon[/eluser]
1. Did you consider cutting out the header area of your page and putting it in a different view? Then one change will reflect across the entire website. You still have to repeat yourself, unfortunately, but that's only one line $this->load->view('header_area');
2. If you want a piece of data to appear in a view, and don't feel like using HMVC or putting any code at all (other than if conditions and echos, of course) in your view (I always feel uncomfortable with that), you must consider generating it with a library and a helper. Make a library to do all the data generation. Use the helper as an interface functions file. That way, you can avoid get_instance() in your view files.