[eluser]PeterGreffen[/eluser]
Here is how I do that:
1. The template library
Create a
template.php in your
system/applications/libraries/ folder.
template.php =
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class template {
var $template_data = array();
function set($name, $value)
{
$this->template_data[$name] = $value;
}
function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
{
$this->CI =& get_instance();
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
return $this->CI->load->view($template, $this->template_data, $return);
}
}
/* End of file Template.php */
/* Location: ./system/application/libraries/template.php */
2. Load the library in you controller(s):
Code:
$this->load->library('template');
or you can autoload it in your
config/autoload.php:
Code:
$autoload['libraries'] = array('template');
3.
Create a new template.php file in your
system/applications/views folder. The contents of that template.php can be something like this:
Code:
<html>
<head>
<!-- add your javascript includes here -->
<!-- add your css includes here -->
<!-- etc -->
</head>
<body>
<div id='header'>
<!-- include your header view here -->
<?php $this->load->view('header'); ?>
</div>
<div id='whateverworks'>
<?= $contents ?>
</div>
<div id='footer'>
<!-- include your footer view here -->
<?php $this->load->view('footer'); ?>
</div>
</body>
</html>
Everytime you load a view, you will load this template view and and another where the $content is. Here's how, in your Controller, instead of using:
Code:
$this->load->view('blog/show_post's);
you can now use
Code:
$this->template->load('template', 'blog/show_posts');
or
Code:
$this->template->load('template', 'blog/show_posts', $data); // :)
That will load the template view file with the view (blog/show_posts) included.
NOTE: make sure the variable name (here that is '
$contents' in your template view, is the same as the one you use in the load() function in your template library in:
Code:
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
Hope that helps!
P.