[eluser]garrettheel[/eluser]
This is a very simple template library for those who want to add a global header and footer to their page as well as having the flexibility to assign variables to the views. Any number of views can also be loaded in the middle of the header and footer. This is the most basic version and I'll update it as I do so in my application.
Template library 0.1
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template {
public $CI;
// Directory that the template resides in, appended to views/ (e.g views/default/).
public $dir = 'default/';
// Views to be loaded on page.
public $views = array();
public $header = array(
"title" => "Default Page Title"
);
public $footer = array();
public function __construct()
{
$this->CI =& get_instance();
}
public function add_view($view, $data = NULL)
{
$page = array($view => $data);
$this->views = array_merge($this->views, $page);
}
public function render()
{
$this->CI->load->view($this->dir . "header", $this->header);
foreach ($this->views as $view => $data):
$this->CI->load->view($view, $data);
endforeach;
$this->CI->load->view($this->dir . "footer", $this->footer);
}
}
Example usage:
Code:
$this->load->library('template');
// Override the default title
$this->template->header['title'] = "My fancy new title";
$this->template->add_view('someview', $somedata);
$this->template->add_view('anotherview', $moredata);
$this->template->render();
I've only left in the 'title' parameter to the header data because this was the only one that pretty much everyone would need. More can easily be added and assigned. Also, it is assumed that the template files are located in /views/default. You can change the location and even make different templates and change which one is used with $this->template->dir = "newtemplate/"
I'm sort of new to CI, so some feedback would be helpful.