[eluser]Francois Gelinas[/eluser]
Hi, I wrote this little extension to the Loader library to standardize my pages layout without having to include 2 files in each of my views (and make it easier to switch to a different layout)
This extension to the Loader library will add a header and a footer to each views, all data passed to the view will also be available to the header and footer views.
By default, the files will be loaded from the layout/default folder in the view folder.
ie: layout/default/header.php and layout/default/footer.php
To use a header and footer from a different layout folder, simply pass the name of the folder as the fourth parameter to the view() function. To load a view without layout, pass FALSE as the fourth parameter.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* Add layout system to load->view (header & footer)
*
* This will embed the requested view with a header and footer file.
*
* By default, the header will be located at : layout/default/header.php
* and the footer at : layout/default/footer.php
*
* To replace the 'default' layout, pass the name as the fourth parameter to the view() function.
*
* To call without a layout, pass FALSE as the fourth parameter.
*
* By Francois Gelinas on june 2009
*
*/
class MY_Loader extends CI_Loader
{
function MY_Loader()
{
parent::CI_Loader();
}
/*
* Override default view function to add layout (header & footer) functionality.
* To get a view without a layout, simply pass FALSE as the fourth parameter.
* To get a different layout, pass the name of of the folder as the fourth parameter.
*/
function view($view, $vars = array(), $return = FALSE, $layout = 'default')
{
$return_value = '';
// process header
if ($layout)
{
$header_file = "layout/{$layout}/header.php";
$return_value .= $this->_ci_load(array('_ci_view' => $header_file, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
// process requested view
$return_value .= $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
// process footer
if ($layout)
{
$footer_file = "layout/{$layout}/footer.php";
$return_value .= $this->_ci_load(array('_ci_view' => $footer_file, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
return $return_value;
}
}