php codeigniter :将数据传递给包含在视图中的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4325633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter : pass data to a view included in a view
提问by Thomas John
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
view_destinations.php
包含一个php菜单文件的视图文件如下
<? $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
?
我的问题是,如何将从控制器获取的数据传递给包含的数据top_menu.php
?
Thank you guys
谢谢你们
回答by castis
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
该view_destinations
文件将有
$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'.
在您的 top_menu 文件中,您应该$otherData
包含“测试”。
回答by thelastshadow
castis's solution works
castis 的解决方案有效
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));
回答by sudhakar phad
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.
对于您的嵌套视图,它已经可用。你可以在你的内心视图中直接访问它。
回答by NaturalBornCamper
Also try this to if you want every single CodeIgniter view data in a subview:
如果您想在子视图中查看每个 CodeIgniter 视图数据,也可以试试这个:
echo $this->view('subview', get_defined_vars()['_ci_data']['_ci_vars'])
回答by Jakub
This Codeigniter forum post should help you ;)
这个 Codeigniter 论坛帖子应该可以帮助你;)
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)
您可以在控制器中将 $data (示例)设为全局,也可以像视图中提到的 @castis 一样传递(变量仅在您的视图中)
More details here: http://codeigniter.com/forums/viewthread/88335/