Skip to content

Displaying Multiple Views

World Wide Web Server edited this page Jul 4, 2012 · 21 revisions

When you try and load more than one View (for whatever reason) in one function, you see that it simply just won't work. So, what do you have to do? Simple!

Find your function (or functions) where you have more than one iew call ($this->load->view('', '', '');), and replace them with:

[code] $output = $this->load->view('your_view', 'your_data', true); $output .= $this->load->view('your_other_view', 'your_other_data', true); $output .= $this->load->view('your_last_view', 'your_last_data', true);

echo $output; [/code]

As you can see, all the View calls are assigned to the $output variable. Also, every View call must have that last bit of TRUE at the end, like so:

Correct:

[code] $output = $this->load->view('your_view', 'your_data', true); $output .= $this->load->view('your_other_view', 'your_other_data', true); $output .= $this->load->view('your_last_view', 'your_last_data', true);

echo $output; [/code]

Incorrect:

[code] $output = $this->load->view('your_view', 'your_data'); $output .= $this->load->view('your_other_view', 'your_other_data'); $output .= $this->load->view('your_last_view', 'your_last_data');

echo $output; [/code]

Also, the first View call must have [code]$output = [/code] before it. Then the View calls after the first one, have to have [code]$output .=[/code] before them.

You can also, alternatively, pass the $output variable to the Output Class that CodeIgniter has (for caching or things like that), like so:

[code] $output = $this->load->view('your_view', 'your_data'); $output .= $this->load->view('your_other_view', 'your_other_data'); $output .= $this->load->view('your_last_view', 'your_last_data');

$this->output->set_output($output); ... your output parsing, caching, etc code here ... echo $this->output->get_output(); [/code]

And that's it, now you can have more than one view in a function.

Clone this wiki locally