-
Notifications
You must be signed in to change notification settings - Fork 0
Displaying Multiple Views
Category:Help::Views | Category:Help::TipsAndTricks [b]NOTE: This article was written before version 1.6. Version 1.6 of CI allows multiple views to be loaded in same controller function.[/b] [h3]Introduction[/h3] When you try and load more than one View (for whatever reason) in one function, you see that it simply just won't work. There are a bunch of ways to do this
See also Displaying Multiple Templates
[h3]Method 1 - Concatenating views[/h3] This method combines the output of all your views sequentially, so for example, you might have a view that sets up your headers, then your content, then your footers.
Find your function (or functions) where you have more than one view 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);
$this->output->set_output($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);
$this->output->set_output($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');
$this->output->set_output($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', true); $output .= $this->load->view('your_other_view', 'your_other_data', true); $output .= $this->load->view('your_last_view', 'your_last_data', true);
$this->output->set_output($output);
... your output parsing, caching, etc code here ...[/code]
[h3]Method 2 - Embedding views[/h3] This method embeds a view within another view.
You can have a view that formats a menu, one that displays the contents of an article, and one that contains the overall template.
[b]views/menu.php:[/b] [code]
-
<?php foreach ($items as $url=>$item) {
echo '
- '.$item.' '; } ?>
[b]views/article.php:[/b] [code]
[/code][b]views/maintemplate.php:[/b] [code]<html> <head> <title><?= $title ?> - My Site</title> <link rel="stylesheet" type="text/css" href="/css/main.css" /> </head> <body>
To use this in your controller (let's pretend $row is the contents of some article pulled from a database):
[code]$menu['items'] = array("/articles" => "Articles", "/authors" => "Authors");
$template['title'] = $row['title']; $template['menu'] = $this->load->view('menu', $menu, true); $article['title'] = $row['title']; $article['author'] = $row['author']; $article['date'] = $row['date']; $article['body'] = $row['body_text']; $template['content'] = $this->load->view('article', $article, true);
$this->load->view('maintemplate', $template);[/code]
How this works is the first two views return output, which is added to our $template array. This array is then passed to the final template, where all the rendering happens.
The benefit of this over method #1 above is that each view contains a more-or-less standalone and relevant chunk of HTML (ie, you don't need a seperate header and footer that have <body> and </body>, respectively). This makes adding things like the wrapper
- Original author: Derek Jones
- How to extend helpers: See User Guide
- Modified by: Thomas Stapleton (id, classes, selected country option and all option)
- Modified by: Bradley De-Lar (construct, setLayout example)