Help with nested views and load->vars

#1
[eluser]neilw[/eluser]
Hello,
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.
Code:
$this->load->view->('myview',$data);

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

e.g.
Code:
$data['name']='bob';
$names=$this->mymodel->get_names();
$data['header']=$this->load->view('header','',TRUE);
$addresses=$this->load->view('partial',$names,TRUE);
$data['content']=$this->parser->parse('content',$addresses,TRUE);
$this->load->view('fullpage',$data);

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.

#2
[eluser]InsiteFX[/eluser]
Code:
$this->load->vars($data);
$this->load->view('fullpage');

Enjoy
InsiteFX

#3
[eluser]neilw[/eluser]
Thanks for the succinct answer Wink

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

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

From the userguide:

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.

#5
[eluser]neilw[/eluser]
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?

Cheers.

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

#7
[eluser]neilw[/eluser]
This kind of thing.

Code:
class Games extends Controller {

    var $data=NULL;
    var $menu=NULL;
        
    function Games()
    {
        parent::Controller();
        $this->load->database();
        $this->load->library('parser');
        $this->load->model('games_model');
        $this->menu=$this->games_model->Get_games_menu_helper();
    }

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

    function status($subtype='finished')
    {
        $this->data['gamesinfo']=$this->games_model->Get_game_and_users($subtype);
        $this->data['gamesinformationlist']=$this->parser->parse('core/gamesdetails',$this->menu,TRUE);
        $this->_standard_games();
    }
    
    function _standard_games()
    {
        $this->data['gamesmenuvertical']=$this->parser->parse('core/gamesmenuvertical',NULL,TRUE);
        $this->load->view('core/startofpage',$this->data);
        $this->parser->parse('gameslistings',$this->data);
        $this->load->view('core/endofpage',$this->data);
    }
}

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