Let's say you load a view from a controller and that view loads another view that uses a lot of the same variables as the view that loaded it. How do get both views to share those variables? Thanks
Asked
Active
Viewed 3,408 times
1
-
@Vickel. Did you try to load view in another view? – Kumar V Jan 18 '14 at 17:26
-
@Vickel. No you didn't try. I have done in my projects to avoid redundancy, we make a single file and loaded in another view file too. – Kumar V Jan 18 '14 at 17:29
-
@kumar_v now I'm learning... can you please post an example? – Vickel Jan 18 '14 at 17:31
-
@Vickel http://stackoverflow.com/questions/9402924/how-to-load-view-into-another-view-codeigniter-2-1 – Kumar V Jan 18 '14 at 17:34
-
2I've just tried it out, you're right, it works! thanks for letting me know something new, very helpful, indeed – Vickel Jan 18 '14 at 17:36
3 Answers
3
All variables you define to a view, are passed down to views loaded within the parent one. You don't need to pass them down an other level through the second array parameter, unless you want to override a specific value.
Basically, define all variable in the 2nd parameter to the "parent" view and both views will have these variables.

Robin Castlin
- 10,956
- 1
- 28
- 44
1
For ex: you are loading view in controller:
$data["msg"] = "hi";
$this->load->view("view_file",$data);
In view_file
, you are loading another view file
$this->load->view("view_file2",array("msg"=>$msg)); // here msg is extracted from first view file

Kumar V
- 8,810
- 9
- 39
- 58
-
-
I mean relay the whole set of variables without having to specify them one by one. – Wes Jan 18 '14 at 18:57
-
1
In one view, i set this:
window.variable= variableToAnotherView;
windows.variable is to pass the variable globaly, so you will be able to call it in another view.