CodeIgniter Forums

Full Version: Pushing data to views with $this->data vs $data?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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?

El Forum

[eluser]InsiteFX[/eluser]
MY_Controller

El Forum

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

El Forum

[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.

El Forum

[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.