Well, here is my attempt to improve the functionality of the template function:
class MY_Loader extends CI_Loader {
public function template($template_name = array(), $vars = array(), $return = FALSE)
{
$content = $this->view('templates/header', $vars, $return);
if (is_array($template_name)) {
foreach ($template_name as $view => $viewVar) {
// Whether the view has different variables
if (is_array($var) && ! is_numeric($view)) {
// Load the view with its own variables
$content .= $this->view($temp, $viewVar, $return);
} else {
// Load the view whith the general variables $vars
// viewVar would be the view name in this case
$content .= $this->view($viewVar, $vars, $return);
}
}
} else {
$content .= $this->view($template_name, $vars, $return);
}
$content .= $this->view('templates/footer', $vars, $return);
if ($return)
{
return $content;
}
}
}
Using this, you could load the views in the format below:
$this->load->template(array(
'first/view' => array('name' => 'value'),
'second/view',
'third/view'
), $generalData);
Each view can have its own variables.
In this case, The first view is loaded by passing the array('name' => 'value')
as its variable. And the second/third views are loaded with $generalData
as the variable.
If you need go get access to $generalData
in the first view, you could use +
operator to merge the variables as: array('name' => 'value') + $generalData
or vice versa.