Doug Molineux
Doug Molineux

Reputation: 12431

CodeIgniter Common Header Include

I am very new to CodeIgniter, but I think I have a grasp on how it works.

Typically when I make a website, like a 5-page site, I have my header file in a php file which is included in each page, so that if I have to alter the header I only have to do it in one spot rather than changing it five times.

In my CodeIgniter application, I have a function for each page in my controller which loads a different view, depending on the function. For example,

    public function Index() {

    $data = array();
    $this->load->view('index',$data);

}

public function blog() {

    $data = array();
    $this->load->view('inner1',$data);

}

Then I can put all my logic in the controller.

What's the best way to have one referenced header? Should I put it in the controller as a variable and then send it as data to each view?

Also, if there's a more efficient way of doing this, please suggest it!

Thanks!

Upvotes: 3

Views: 13897

Answers (3)

fedeisas
fedeisas

Reputation: 2023

I use CodeIgniter a lot. I have a folder named /common inside my /views folder. There, I put my footer.php, sidebar.php, header.php, and so.

My view files looks like:

$this->load->view('header');
<div id"main">
.....
</div>


$this->load->view('footer');

My controller files looks like:

public function blog() {
   $this->data['title'] = "Blog";
   $this->data['meta_tags'] = $meta;
   $this->data['entries'] = $entries;

   $this->load->view('blog',$this->data);
}

Make note that the variables as $title or $meta, can be later called within the header template. This way, I can make changes on common/header.php and the persist across the site.

Good luck!

PD: I recommend using Carabiner for managing your assets at the top of every controller and then display them on your header.php

Upvotes: 5

someoneinomaha
someoneinomaha

Reputation: 1304

Have you looked at any of the template projects out there?

http://williamsconcepts.com/ci/codeigniter/libraries/template/

http://philsturgeon.co.uk/index.php/code/codeigniter-template

These would allow you to create a master template (or more if you needed) where you could make site-wide changes in a single file. Those changes could include the header, but they could include other regions as well.

Upvotes: 1

Bill
Bill

Reputation: 5668

What I usually do is make the header in a view, and then add the header view to the controller above index... you can do the same with footer too.

So it would be something like:

public function blog() {

   $data = array();
   $this->load->view('Header'); // just the header file
   $this->load->view('inner1',$data); //your main content
}

Make sense?

Added: You can also include your entire head tag in there too, like your meta tags, title, css links etc. But I usually put those in another view because sometimes they are different depending on the page.

Upvotes: 5

Related Questions