CodeIgniter – Multiple Views

If you want a main header and footer file (like WordPress) with variables passed from the controller then continue reading. The content below simply displays this exact layout.

The controller file holds an array with array so on the first view file, the second array can still be passed, thus keeping organization between the header, the main page, and the footer.

Some Controller File

<?php
// Create a 2 dimensional to pass variables.
class page extends Controller {
	function index() {
		$data['header']['title'] = 'Title Tag';
		$this->_loader("home", $data); // Home is the main view page
	}
}
?>

The $header and $footer variables are based on the array that was passed initially. Remember the array keys get converted to the actual variable name when its passed onto the viewer.

Main View Page

<?php $this->load->view("header", $header); ?>
 
MAIN CONTENT
 
<?php $this->load->view("footer"); ?>

Header File

....
<title><?php echo $title; ?></title>
....

Pay attention to $data['header']['title'] and see how the data travels through both views.