Help with nested views and load->vars

In my code I need to use nested views. The solution I came up with is as follows: In the controller I dump all the data I need into an associative array then call a single view representing the whole page but made up of this data, e.g.

I populate the $data from a selection of:
- model functions,
- from a view using the TRUE parameter
- from the template parser using the TRUE parameter


On searching the forum I came across a technique that used $this->load->vars but I couldn't really figure it out and how it differed from my technique.

Can somebody explain what exactly load->vars is and how it differs from the method I described, or perhaps why my method is wrong. I'm kind of conscience that I'm storing a fair amount of data until the page is displayed and I could be doing it all wrong.

Many thanks.



Thanks for the succinct answer Wink

But how is that different than passing in the variable to the view?

[eluser]Ben Edmunds[/eluser]
Hey neilw,

From the userguide:


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.

Right. I think I follow. In one of my classes I wanted to set the data but because it was in different methods I set $data as an instance variable. I guess from the above I could just remove the instance variable and use load->vars instead? I'm assuming (not at my dev computer for the next few days) that I call load->vars in each method and the data is loaded into the global area until the end of the response?

btw, is my method of grabbing all the data in the original post ok?


[eluser]Ben Edmunds[/eluser]
You might have to give me some example code for me to understand what you mean...

Basically if you are loading multiple views you can use this->load->vars so you dont have to worry about passing data to the multiple views.

This kind of thing.

class Games extends Controller {

    var $data=NULL;
    var $menu=NULL;
    function Games()

    function index()
    { $this->status(); }

    function status($subtype='finished')
    function _standard_games()

[eluser]Ben Edmunds[/eluser]
You could definitely use $this->load->vars with this. I don't see any documentation on whether or not the parser library supports it but I would think it would.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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