2

Controller:

public function home(){
$data['page_title'] = 'Home';
$data['navbar_content'] = $this->model->get($var);
$data['page_content'] = $this->model->get($var1);

$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');
}

Using the sample code above, Since codeigniter only allows the passing of variables/data through an array or object from controller to the view then what is the 'right' way to pass my data?

I could simply pass the data when loading the header and things will still work out fine.

$this->load->view('template/header', $data);
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');

But that doesn't seem 'right' to me, i guess i wanted things to be clear in my code for example:

$this->load->view('template/header', $title);
$this->load->view('template/navbar', $navbar_content);
$this->load->view('pages/home', $page_content);
$this->load->view('template/footer');

The above is clear cut and there is a distinction to the data that are being passed.

But since i can't do that, is there a 'correct' way of doing this? or am i thinking too much on it?

I neglected to mention, I could simply make 2 arrays to pass to the view. I thought this is wrong in the first place since i'm going out of my way to make 2 arrays to pass just so i can pass a single string variable to my header page.

Ofcourse when i pass more data to my navbar then it would make sense to me.

Awing
  • 59
  • 5

2 Answers2

1

This is the correct way according to the documentation: https://codeigniter.com/userguide3/general/views.html

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method.

You should also look on: Codeigniter: Best way to structure partial views

You should structure you views hierarchically.

$this->load->view('template/header', ["title" => $title]);
$this->load->view('template/navbar', ["navbar_content" => $navbar_content]);
$this->load->view('pages/home', ["page_content" => $page_content]);
$this->load->view('template/footer');
Andrei Lupuleasa
  • 2,677
  • 3
  • 14
  • 32
  • But i needed to pass data to my header. And that's just for now, i'll probably need to pass data to my navbar. wouldn't it be redundant if i passed data to both loading statements? it also wouldn't make sense for me to make 2 arrays so i can do this? – Awing Mar 14 '19 at 07:17
  • So pass it as an `array` or as an `object`. – Andrei Lupuleasa Mar 14 '19 at 07:18
  • Can you edit your answer and show me how you would do so? – Awing Mar 14 '19 at 07:19
  • You want to pass the same `data` to multiple `views` ? – Andrei Lupuleasa Mar 14 '19 at 07:19
  • My problem was that, i could simply put all the data i needed in one array and pass that array in the first loading statement(header in this case) and it will be useable by all other views loaded after it as they are all loaded on the same page. Since i needed to pass 1 string variable to my header page as title, i passed it there since if i passed all the data on my page content the php code i had in header view would error out as the header is loaded first before the content where the data is passed. also i wanted a clear cut passing of data to views relevant to it, for example – Awing Mar 14 '19 at 07:33
  • my title data i wanted it to pass to my header loading statement, navbar content to my navbar loading statement and page content to my page loading statement. instead of just a single general data array, its not exactly readable in my controller code without looking at the relevant views code. – Awing Mar 14 '19 at 07:34
  • So pass multiple arrays with relevant data for those views. They can be arrays of one element if you don't have more data for the moment. – Andrei Lupuleasa Mar 14 '19 at 07:37
  • I will probably have to do that everytime i load a single webpage. If so, would it just make sense for me to just use a single array and just pass it to the header instead of seeking readability like i wanted? – Awing Mar 14 '19 at 07:53
  • There is no absolute answer for what you seek, you will have to make refractories sooner or later. – Andrei Lupuleasa Mar 14 '19 at 08:12
  • Then i'll just choose the readability route. it also coincides with my plan to write more readable codes, the other stuff comes later. baby steps. – Awing Mar 14 '19 at 08:53
0

CodeIgniter passes data from controllers to views.

You need to pass $data to views.

You will pass an array with its keys be strings or even arrays (multi-dimensional arrays also).

So, you need to pass it to every view you are using $data.

$this->load->view('template/header', $data);
$this->load->view('template/navbar', $data);
$this->load->view('pages/home', $data);
$this->load->view('template/footer', $data);

And in your view, you can access $data with keys.

For example:

if your data is

$data['header_banner'] = 'Header Banner';

In your view, you can access this value by:

echo $header_banner;

Observe that to the key in $data, you just need to add a $ and you can access that variable.

Its something line PHP's in built function extract()

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • I get that, but it would also suffice to just pass $data once to header and all other views can access it since it all renders on the same page and header is rendered first thus the data is already there when the other views are loaded. im just now realizing maybe i'm making a fuss out of nothing. – Awing Mar 14 '19 at 07:22
  • Good Question!!! Actually, you are not loading all views in one statement. You are loading views individually so, every time, you need to pass data to view. – Pupil Mar 14 '19 at 07:22
  • What i meant is that, i took all my data that i need to pass to my view and put it inside 1 array as $data then passed $data to my load header statement like in the above question i put it there and the rest of my views can use the data passed from the header since it all loads on the same page and header is loaded first. – Awing Mar 14 '19 at 07:27