[eluser]missionsix[/eluser]
I've had a similar problem.
All my pages are wrapped with similar header / footers. So calling those views on every controller page seemed like a lot of code duplication.
So what I did was create a Parent controller that all my other controllers would extend. Then I created 1 method called Display that would load the header / footer views, or any other 'global' view files and then spit everything out into a container view.
Example: Public_Controller.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Public_Controller extends Controller {
public $container;
public $data = array();
public function Public_Controller() {
parent::Controller();
$this->container = 'container';
}
private function _prepare() {
// Here I assign global data, like userinfo, ad management, ect...
[...]
}
public function display($data = array()) {
$this->data = array_merge($this->data, $data);
// do any global data preparation
$this->_prepare();
$this->data['header'] = $this->load->view('includes/header', $this->data, true);
$this->data['footer'] = $this->load->view('includes/footer', $this->data, true);
$this->load->view($this->container, $this->data);
}
}
?>
And then in my other controllers I do something like this:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
include_once 'public_controller.php';
class Front extends Public_Controller {
public function __construct()
{
parent::Public_Controller();
[...]
}
public function index() {
// do index page
$this->display();
}
}