Sharing data between view partials |
[eluser]Isuka[/eluser]
Hi, When I build an app, I usually create a master view like this : Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> In my controller I make something like this : Code: class Welcome extends Controller So I'm wondering if it's possible to have a global $data accessible for all my views (master, content and sidebar). (I used to make partial with the great View Library by coolfactor before, but there's not update since July 2007, so I try to use the default CI View Library now ) Thanks for any helpful information
[eluser]darrenm[/eluser]
I use a slightly different approach. I abstract my view loading into a custom library called Page. This lib has functions for adding and removing style sheets and javascripts and a few other things. It then has a function called render_page() which loads all the various views required Code: function render_page($view,$data) { you can then call this from your controller like so: Code: $data['docTitle'] = 'My document title'; This keeps all $data in one array and has lots of scope for handling other common tasks (I use it in conjunction with a navigation generator for example). Hope that helps
[eluser]Isuka[/eluser]
Your approach is like the View Library I used before and it works pretty well, so I think I'm going to not change my habits and stay with it. Maybe in CI 2.0 I will have a view library I want to use
[eluser]John_Betong[/eluser]
Hi, >>> When I build an app, I usually create a master view like this : Try this instead Code: class Welcome extends Controller
[eluser]xwero[/eluser]
If you use load->vars the title will be 'global' Code: $this->load->vars(array('title'=>'My Great Title'));
[eluser]xwero[/eluser]
John a tip : don't give all your arrays the same name. In the example you show the title key will be passed on to the master view too which is not needed. load->vars does the same thing, added for the people who pay attention
[eluser]Isuka[/eluser]
@John_Betong : Great tips, didn't think about that It work well with my exemple but with a more complex controller it might be tricky. @xwero : I never used the load->vars method before, but it's exactly the method I need for what I want to do. Thanks !
[eluser]John_Betong[/eluser]
Hi @Xwreo and @Isuka, I have amended my original post and removed $data where it was unnecessary. @Isuka in complex controllers it does get tricky, just ensure all your variables are declared before loading the partial views otherwise some weird errorrs appear which are not easy to track down. Keep your code clean and simple so it is easy to read later and to easily add Code: echo '$problem_variable --->' .$problem_variable .<---'; die; By the way, I am from the old school (with umpteen T-shirts) that shy away from variables that are global and prefer using locals instead. Far easier to trace problems.
[eluser]xwero[/eluser]
With you new code you are not adding the partials to the master view file. Code: $glob_partials['title'] = 'My Great Title'; Code: $glob_partials['title'] = 'My Great Title';
[eluser]John_Betong[/eluser]
Hi Xwero, I like your first example it is easy to read and understand at a glance. Your second example is a wee bit too complex for me and not easy to read and understand at a glance. I will endeavour to utilise your first example. Many thanks for the tip. |
Welcome Guest, Not a member yet? Register Sign In |