-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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:
$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);
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:
$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);
Incorrect:
$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);
Also, the first View call must have php $output =
before it. Then the View calls after the first one, have to have ```php
$output .=
You can also, alternatively, pass the $output variable to the Output Class that CodeIgniter has (for caching or things like that), like so:
```php
$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 ...
[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]
<ul>
<?php foreach ($items as $url=>$item) {
echo '<li><a href="'.$url.'">'.$item.'</a></li>';
}
?>
</ul>
[b]views/article.php:[/b]
<div id="article">
<h1><?= $title ?></h1>
<span class="date"><?= $date ?></span>
<span class="author"><?= $author ?></span>
<p><?php echo nl2br( str_replace("\n\n","</p><p>", $body )); ?></p>
</div>
[b]views/maintemplate.php:[/b]
<html>
<head>
<title><?= $title ?> - My Site</title>
<link rel="stylesheet" type="text/css" href="/css/main.css" />
</head>
<body>
<div id="wrapper">
<div id="header">My Embedded-View Site</div>
<div id="menu"><?= $menu ?></div>
<div id="content"><?= $content ?></div>
<div id="footer">© 2006<?php $endyear = date("Y"); if ($endyear != 2006) echo '-'.$endyear; ?> Me</div>
</div>
</body>
</html>
To use this in your controller (let's pretend $row is the contents of some article pulled from a database):
$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);
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