There have been a few discussions in these boards regarding embedding views within other views (what RoR apparently calls partials), if say, you want certain elements like headers, footers, etc. to be used automatically with your pages.
There seems to be some confusion as to how this might be accomplished so I thought I would try to clear it up by describing one way to do this. There are others, but this to me is the simplest.
CI has a very handy function called: $this->load->vars()
This function lets you set variables, which will be available to any of your views.
Let’s say you have a view called container, with this in it:
<?php
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('content');
$this->load->view('footer');
?>
Each of those views, in turn, contains variables or other dynamic structures. For example, the header template might look like this:
<html>
<head>
<title><?=$heading?></title>
</head>
<body>
And the content template might expect a database result:
<div id="content">
<?php foreach($content->result() as $row): ?>
<div class="news">
<h3><?=$row->title?></h3>
<p><?=$row->information?></p>
</div>
<?php endforeach; ?>
</div>
In your controller you can set the data for every single one of the views simultaneously just by doing this:
$data = array(
'heading' => 'My News Page',
'sidebar' => $this->db->get('sidebar'),
'content' => $this->db->get('content')
);
$this->load->vars($data);
$this->load->view('container');
