• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pushing data to views with $this->data vs $data?

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?


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

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.

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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

Users browsing this thread:
1 Guest(s)

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.