User Tools

Site Tools


php:layout_codeigniter

How to create a layout in CodeIgniter

Oct 2012

As you probably noticed already, Codeigniter doesn't come with a default layout system. On the other hand, simulating a layout is very easy and can be achieved in two different ways.

The first way is to transform a regular view into a layout, and call it systematically from your controllers. Even if this methods works well, you'll be in need to load every piece of your layout within your controller's method (using $this→load→view with a third parameter, to return the view as data). Check below for a quote from CI User Manual:

There is a third optional parameter lets you change the behavior of the function so that it returns data as a string 
rather than sending it to your browser. This can be useful if you want to process the data in some way. 
If you set the parameter to true (boolean) it will return data. The default behavior is false, 
which sends it to your browser. Remember to assign it to a variable if you want the data returned:

$string = $this->load->view('myfile', '', true);

A second (better) approach is to use a “hook” for your view, and automagically integrate it into a predefined layout. How do you do this? It's quite simple actually…

  • Create a file in hooks/ called layout.php

hooks/layout.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * CI Layout
 *
 * 
 */
class Layout {
    public function index()
    {
        $CI =& get_instance();
 
        $current_output = $CI->output->get_output();
 
        $layout_file = APPPATH . 'views/layout.php';
        $layout = $CI->load->file($layout_file, true);
        $mod_output = str_replace("{content}", $current_output, $layout);
 
        echo $mod_output;
    }
}

  • Create the actual layout file (as a view). Must match the $layout_file from above.

Important here is the {content} string. This will be replaced with our view.

views/layout.php

<!DOCTYPE html>
<head lang="en">
    <meta charset="UTF-8">
    ...
</head>
<body>
    <div id="page_container">
        <nav id="main_menu">
            <h2>Main Navigation</h2>
            ...
        </nav>
 
        <section id="main_content" role="main">
            {content}
        </section>
 
        <footer id="main_footer">
        ....
        </footer>
    </div>
</body>
</html>

  • Modify config/hooks.php and enter a display_override hook.

This will call the code above every time a view is going to be displayed.

Creating a hook of display_override type

$hook['display_override'] = array(
    'class' => 'Layout',
    'function' => 'index',
    'filename' => 'layout.php',
    'filepath' => 'hooks'
);

That's pretty much all of it. If you want different layouts depending of your controller, you can add a controller variable with the name of the layout, and modify accordingly the Layout→index() method to take care of it ($CI→layout_name will be accessible there). Implement an extra function to check for the actual existence of the layout and eventually a default one (if the specified one was invalid). It pretty much depends on what you need, but the example here will cover the most simple situations.

php/layout_codeigniter.txt · Last modified: 2016/09/08 16:47 (external edit)