开发者

codeigniter : pass data to a view included in a view

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

$this->load->vie开发者_运维知识库w('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


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'.


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));


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/


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.


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'])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜