Welcome Guest, Not a member yet? Register   Sign In
Loading multiple views concurrently - $data array can't be used unless it's loaded in the first!
#1

[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' );
$this->load->view( 'hello', $data );
$this->load->view( 'sidebar' );
$this->load->view( 'footer' );

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...
#2

[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
#3

[eluser]stormbytes[/eluser]
This should definitely be somewhere in the UG -
#4

[eluser]InsiteFX[/eluser]
Read this in the Users Guide!

Loader Library

Code:
$data['Title'] = 'My Title';

$this->load->vars($data);

$this->load->view( 'header' );
$this->load->view( 'hello' );
$this->load->view( 'sidebar' );
$this->load->view( 'footer' );

Now the data array is avaible to all views!

InsiteFX
#5

[eluser]stormbytes[/eluser]
Excellent suggestion - Thanks! Smile

The manual says...
Quote:$this->load->vars($array)

This function takes an associative array as input and generates variables using the PHP extract function. This function produces the same result as using the second parameter of the $this->load->view() function above. The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.

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?
#6

[eluser]InsiteFX[/eluser]
Yes, this would allow you to pass say a $param array through the constructor.

InsiteFX
#7

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

[eluser]stormbytes[/eluser]
[edit]

Phil -

Thanks for the post. I'll have to review what you wrote a couple of times Smile 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';
$this->load->view('header', $data);

And then subsequently, in the same function, a second statement:

Code:
$data['var'] = 'bar';
$this->load->view('page', $data);

Because of the sequentiality of interpreting the class, I'll actually have $var = 'foo' in 'header', and $var = 'bar' in page.

Makes sense..




Theme © iAndrew 2016 - Forum software by © MyBB