Reputation: 12038
I'm wondering how $this->load->vars()
works in CodeIgniter. The documentation is fairly vague about it.
I have the following code:
$init = $this->init->set();
$this->load->view('include/header', $init);
$this->load->view('include/nav');
$dates = $this->planner_model->create_date_list();
$this->load->view('planner/dates_content', $dates);
$detail = $this->planner_model->create_detail_list();
$this->load->view('planner/detail_content', $detail);
$this->load->view('include/footer');
However, I also need the $dates
array in my detail_content
view. I was trying to load it with $this->load->vars()
and hoping it would append to the $detail
array, because the CI documentation states as follows:
You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.
Would it work if I do $detail['dates'] = $dates;
? Will it append the $dates
array to $detail['dates']
then?
Thanks in advance.
Upvotes: 4
Views: 10073
Reputation: 4984
As is stated in other answers, and in the user guide, using $this->load->vars()
is the same as including the second argument in $this->load->view()
.
But from the user guide:
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.
This to me, is the only reason you'd use $this->load->vars()
. As @madmartigan says, it's more convenient to use the view loader with the second argument.
Upvotes: 2
Reputation: 102745
$this->load->vars()
is perfect for this purpose. Try this:
$init = $this->init->set();// Won't be passed to the next 2 views
$this->load->view('include/header', $init);
$this->load->view('include/nav');
$dates = $this->planner_model->create_date_list();
$this->load->vars($dates);
$this->load->view('planner/dates_content');
$detail = $this->planner_model->create_detail_list();
$this->load->vars($detail);
$this->load->view('planner/detail_content');
What looks strange to me is that normally you pass an associative array as data, like $data['my_var_name'] = $var_value
, so I assume your model calls are returning the data already structured with the variable names (array keys) that you'll use in your view which I do find odd, but then I know nothing of your application.
Here's a more "conventional" version:
$data['dates'] = $this->planner_model->create_date_list();
$this->load->view('planner/dates_content', $data);
$data['detail'] = $this->planner_model->create_detail_list();
// receives both dates and detail
$this->load->view('planner/detail_content', $data);
Upvotes: 5
Reputation: 21174
Have you tried just just building an array that you pass to the different views? I find $this->load->vars()
behaves unexpectedly.
Upvotes: 2