2

Hi can anyone let me know which is the best way to use the header footer in codeigniter? The way below seems to be troublesome to put into each function to display header footer

$this->load->view('header');
$this->load->view('content', $data);
$this->load->view('footer');

Also i browse around the web i see some people do the loading of header and footer functions in the Core controller. Anyone have any idea how to do it?

Please advise anyone.

Pramod Kumar Sharma
  • 7,851
  • 5
  • 28
  • 53
Eugene Tang
  • 83
  • 2
  • 8
  • 1
    Use a template view as shown in this example http://stackoverflow.com/a/15593888/1592648 Basically you load the template view which will load the header, footer and content views, and which will pass your `$data` var along to them. This way you do not need to make a method as shown below, which you would have to re-create in all your controllers unless you override core. Having a single template file makes it effortless as you just load it in the controller as you would any other view. – kittycat May 24 '13 at 14:08
  • please see this http://stackoverflow.com/questions/9540576/header-and-footer-in-codeigniter – Renish Khunt Sep 21 '14 at 15:24

2 Answers2

5

in your core controller create a method & load your header view & footer view

  public function load_view($view, $vars = array()) {
    $this->load->view('header', $vars);
    $this->load->view($view, $vars);
    $this->load->view('footer');
  }

in your controllers, call

$this->load_view('my_view', $view_data);

if you don't need header & footer (for example ajax views), you can normally call

$this->load->view('my_ajax_view', $view_data);
Dino Babu
  • 5,814
  • 3
  • 24
  • 33
0

I have created a repo CodeIgniter-Assets on github to solve custom header/footer problem with codeigniter which is really easy to configure I hope this will solve your issue.

shaz3e
  • 316
  • 2
  • 14