Welcome Guest, Not a member yet? Register   Sign In
Best way to load header and footer once globally?
#7

I would suggest to make MY_Loader.php in application/core folder. That automagically extends load method and you don't need to extend any controller.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Loader extends CI_Loader {

function __construct() {
parent::__construct();

$CI =& get_instance();
$CI->load = $this;
}

// this is used in cms
public function template($template_name, $vars = array(), $return = TRUE){
$content = $this->view('header', $vars, $return);
$content .= $this->view('leftpanel', $vars, $return);
$content .= $this->view('rightpanel', $vars, $return);

if(is_array($template_name)) { //return all values in contents
foreach($template_name as $file_to_load) {
$content .= $this->view($file_to_load, $vars, $return);
}
}else{
$content .= $this->view($template_name, $vars, $return);
}

$content .= $this->view('footer', $vars, $return);

echo $content;
}
}

Then call it from controller like this:

$this->load->template('dashboard', $data);

And you can use multiple templates using array like:

$this->load->template(['dashboard', 'bottom_content'], $data);
Reply


Messages In This Thread
RE: Best way to load header and footer once globally? - by davis.lasis - 10-04-2018, 10:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB