I am currently develop a website application in codeigniter, and I'd like to do something in PHP / CodeIgniter where I can make a common template for separate sections of the website. I was thinking that I would keep the header / footer in a separate php files & include them separately.

The thing I'm not sure about is the content beneath the header and above the footer. This website application will contain a lot of different pages, so I'm having a hard time figuring how what's the best way to do this.

  • i've been in this road , do yourself a favor and use a template engine ... i recommend blade ( laravel engine ) you can find a CI version on github
    – max
    Jan 5, 2016 at 13:36

4 Answers 4


I generally prefer to create a "layout" file rather than having to include both a header and footer on every page. It's more flexible.

Here's a snippet from one of my projects:

include '../views/'.$templateFile;
$pageContent = ob_get_clean();
include '../views/layouts/'.$layoutFile;

All you have to do is enable output buffering, include the template, then call ob_get_clean() to nab the contents of your template and put it into a variable. Once it's in a variable you can include your main layout file, which should echo $pageLayout somewhere inside.


    <title>Your Site</title>
    <!-- header here -->
    <?= $pageContents ?>
    <!-- footer here -->

That said, surely Code Igniter has some kind of templating built in, no? I'm not familiar with it. Edit: Apparently it does not. Kind of silly really; with caching, the performance cost of a nice templating engine is negligible.


You can use Hook , or giving the parameter in every your method. Example :

Class My_controller extends CI_Controller{

    public function index(){
        $data = array(
            'container' => 'contain'

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


and also give your base_view.php (as a main view) the value from your sended variable.

    <title>Your title here</title>

    <section id="container">
        <?php $this->load->view($container); ?>


Hope that helps..

  • don't forget to create contain.php in your "view" folder to be loaded Jun 28, 2013 at 2:20

Something I use most of the time, is just three (or more) views in the controller, like this:

class Mycontroller extends CI_Controller {
    public function index() {
        $headerData['title'] = 'Title of the page';
        $yourData['content'] = 'Everything you want to do inside your controller';


This way you can also set, for example, a title in your header file.

edit: you can extend this with, for example, menu views or something to keep things even more seperated. Just make sure that you load the views in correct order.


I always used to create systems with CodeIgniter and this template library: http://www.williamsconcepts.com/ci/codeigniter/libraries/template/

It's very simple to use and it can do a better job than yours used techniques like using only $this->load->view() method.

  • 1
    Your answer doesn't address all of the OP's concerns, especially with respect to the content between the header and footer. Your answer would be stronger if you explained why the linked template helps address the OP's concerns.
    – user53019
    Jun 28, 2013 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.