Welcome Guest, Not a member yet? Register   Sign In
Passing in a array into a sub-view
#1

[eluser]msteudel[/eluser]
I have something like the following:

Code:
<?php $this->load->view( 'photo', array( 'iso' => $iso ) ?>

In my photo view I do the following:

Code:
<?php var_dump( $iso ) ?>

and I get:

Code:
Severity: Notice

Message: Undefined variable: iso

I also tried doing something like:
Code:
<?php echo $secondData['votes'] = $num_votes ?>
<?php $this->load->view( 'photo', $secondData ) ?>

I'm not sure why it's not allowing me to pass data into my views ...
#2

[eluser]techgnome[/eluser]
Something I recently discovered, is that variables available to the view are also available in the sub view w/o needing to pass them again...

Try loading your view like this:
Code:
<?php $this->load->view('photo'); ?>

And use the var_dump($iso); in your photo view, see if you get a different result.

-tg
#3

[eluser]msteudel[/eluser]
That's what I thought too, but I got the undefined error, which is why I started trying to pass in the data ... I must be doing something not normal ... but I can't figure it out.
#4

[eluser]Dennis Rasmussen[/eluser]
[quote author="techgnome" date="1286159186"]Something I recently discovered, is that variables available to the view are also available in the sub view w/o needing to pass them again...[/quote]

What?? Since when?
Where do you see that change? :O
#5

[eluser]InsiteFX[/eluser]
From the userguide:
Code:
$this->load->vars($data);
$this->load->view('photo');
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.

Enjoy
InsiteFX
#6

[eluser]Dennis Rasmussen[/eluser]
That is just pure love <3
If just I knew that long time ago Smile
#7

[eluser]InsiteFX[/eluser]
It's a little hard to find, because it is under the loader library.

Loader Library

But here is the link to it.

InsiteFX
#8

[eluser]techgnome[/eluser]
[quote author="Dennis Rasmussen" date="1286161413"][quote author="techgnome" date="1286159186"]Something I recently discovered, is that variables available to the view are also available in the sub view w/o needing to pass them again...[/quote]

What?? Since when?
Where do you see that change? :O[/quote]

Discovered by accident... my controllers all load a common template, then inside the template, I load sub views. I had been passing an array to the sub-views, but was running into a problem setting them up properly. Through some accidental experimentation, I found all my variables from the main view available to me in the sub views.

-tg
#9

[eluser]Dennis Rasmussen[/eluser]
[quote author="InsiteFX" date="1286167591"]It's a little hard to find, because it is under the loader library.

Loader Library

But here is the link to it.

InsiteFX[/quote]
That explains it.
I've read every bit and part of the sourcecode and userguide EXCEPT for the loader library.
Talk about no luck Smile

[quote author="techgnome" date="1286187990"]Discovered by accident... my controllers all load a common template, then inside the template, I load sub views. I had been passing an array to the sub-views, but was running into a problem setting them up properly. Through some accidental experimentation, I found all my variables from the main view available to me in the sub views.

-tg[/quote]
Yeah I did this as well for some months, then I recently went over to loading views in the controller, but seeing it's possible to pass global view-variables, I'll head back to views + subviews (as I believe it's up to the designer to sort all of this out).

All in all, great to know Smile




Theme © iAndrew 2016 - Forum software by © MyBB