codeigniter : pass data to a view included in a vi

2019-02-03 04:23发布

问题:

I have a controller and including two views from one function as below

$this->load->view('includes/header',$data);
$this->load->view('view_destinations',$data);

The view file view_destinations.php including a php menu file as follows

<? $this->load->view('includes/top_menu'); ?>

My question is, how can I pass data that is fetched from the controller to this included top_menu.php ?

Thank you guys

回答1:

Inside your controller, have

$data['nestedView']['otherData'] = 'testing';

before your view includes.

When you call

$this->load->view('view_destinations',$data);

the view_destinations file is going to have

$nestedView['otherData'];

Which you can at that point, pass into the nested view file.

<? $this->load->view('includes/top_menu', $nestedView); ?>

And inside your top_menu file you should have $otherData containing 'testing'.



回答2:

castis's solution works

however if you want to do this on a more finely grained level you can use:

//in your controller
$data['whatever'] = 'someValue';

.

//In your view
echo $whatever //outputs 'someValue';

//pass $whatever on
$this->load->view('some/view', Array('whatever' => $whatever));


回答3:

This Codeigniter forum post should help you ;)

You can either make your $data (example) global in the controller, or pass just like @castis mentioned from within your view (variables only in your view)

More details here: http://codeigniter.com/forums/viewthread/88335/



回答4:

I have seen in my view files, if I'm passing data from controller to view and from that view to included nested view files. there is no need to transfer

$data

for your nested view it is already available. you can directly access it, within your inner view.



回答5:

Also try this to if you want every single CodeIgniter view data in a subview:

echo $this->view('subview', get_defined_vars()['_ci_data']['_ci_vars'])