Loading multiple views concurrently - $data array can't be used unless it's loaded in the first! |
[eluser]stormbytes[/eluser]
So, referencing the UG - http://ellislab.com/codeigniter/user-gui...views.html When loading multiple views.. Eg.: Code: $this->load->view( 'header' ); The $data array (variables) are only accessible *after* the load statement which included $data has been 'processed'. Meaning, if you have header elements that depend on $data-variables, you'll get an error. The way I've remedied this is by loading $data into the very first load statement (eg - $this->load->view('header', $data). I'm wondering if this is something the dev team will want to change in the upcoming release of v.2. or if I'm overlooking something? My understanding was that everything is pre-buffered, in which case it shouldn't make any difference what order the Load-statement containing the $data array appears in. Curious to hear thoughts...
[eluser]techgnome[/eluser]
The way I understand it, the output maybe buffered... but the processing isn't. So if you need something in the header, it needs to be passed in at that point. -tg
[eluser]InsiteFX[/eluser]
Read this in the Users Guide! Loader Library Code: $data['Title'] = 'My Title'; Now the data array is avaible to all views! InsiteFX
[eluser]stormbytes[/eluser]
Excellent suggestion - Thanks! The manual says... Quote:$this->load->vars($array) So here's my follow up question - I'm a bit fuzzy when it comes to variable scope & encapsulation, especially with CI. The manual says that I can place this call in the Constructor to make it available to all the functions (methods) of that class. So then, I can put this call in my Constructor and pretty much forget about it, knowing that all my $data values will be available to any view in a SuperGlobal. That about right?
[eluser]InsiteFX[/eluser]
Yes, this would allow you to pass say a $param array through the constructor. InsiteFX
[eluser]Phil Sturgeon[/eluser]
Data is stored in $this->load->_cached_vars after $this->load->view() or $this->load->vars() has been run, which means all later view files have access to it. That is what the user guide means by saying $this->load->vars() is the same as load view. When you load a view, it includes the PHP file, runs an extract on $this->load->_cached_vars so $this->load->_cached_vars['foo'] becomes $foo, then stores the HTML. If a variable is not yet available, it will not work. doing things this way means that if I include a view with $data['foo'] containing 'bar', then override that value in a second view call with 'baz' it will show 'bar' in the first view then 'baz' in the second. If they post-processed all the data against the view at the end then I would only ever see 'baz' in both locations. It might seem illogical initially, but it makes a LOT of sense if you think about it that way. Anyway, have a look at my CodeIgniter Template library for a much nicer way to handle templates. Just added in mobile support and some nice global data handling. Code: $this->template->foo = 'bar'; Shove that in your constructor, controller, wherever to make it globally available in your header, footer, main body, sidebar partials, whatever.
[eluser]stormbytes[/eluser]
[edit] Phil - Thanks for the post. I'll have to review what you wrote a couple of times It's slightly above my pay-grade at this point. I take it you're explaining why CI has to follow the steps it does, which in part is why I'm having the issue I described. That's fine. I'll simply keep an eye out for it, making sure my array is included in the first load->view statement. Actually I think I get it - You're saying the way they've got it now, if I have the following statement: Code: $data['var'] = 'foo'; And then subsequently, in the same function, a second statement: Code: $data['var'] = 'bar'; Because of the sequentiality of interpreting the class, I'll actually have $var = 'foo' in 'header', and $var = 'bar' in page. Makes sense.. |
Welcome Guest, Not a member yet? Register Sign In |