Friday, April 17, 2015

Using View Layouts in CodeIgniter

I'll show you how tp extend CodeIgniter's loader to enable layout views.

CodeIgniter doesn't use a template engine. I think their argument that PHP is already a templating language is acceptable for small projects - my team is just me, templates would just add to project complexity and maybe impact performance.

But I do miss the concept of layouts. Instead, codeigniter uses this idiom:

    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);
    $this->load->view('templates/footer', $data);

That's ugly. As your site grows, and you need to sync css to javascript libraries between headers and footers, this results in maintenance pains. I'd prefer to embed my content in a layout:

  <html>
    <head>
        <title>Hello</title>
    </head>
    <body>
        <h1>Hey!</h1>
        <div class="content">
        <?php echo $content; ?>
        </div>
        <hr />
        <em>© 2014</em>
    </body>
</html>

And then invoke like this ...

    $this->load->setLayout('layouts/main');
    $this->load->view('pages/'.$page, $data);


Well, it's pretty simple to extend CodeIgniter to do this by customizing the view load behavior. First, we'll create the class ./application/core/MY_Loader.php (MY_ is our customization namespace), and add a couple member variables:

    class MY_Loader extends CI_Loader {

        var $layout = '';
        const EXT = '.phtml';

    }

The $this->layout variable holds the file path to the layout. I am also using a different file extension for my views. This keeps me mindful not to burden my views with too much logic. It's too easy to get mixed up when with the views and code have the same file extension.

 Now the constructor:

    function __construct() {
        parent::__construct();
        
        $layout = config_item('layout');
        if ($layout <> '') {
            $this->layout = $layout.self::EXT;
        }
    }    

Make sure to call the core class constructor, and fetch a default layout value from the site configuration (we'll see how to add that below). We'll add a convenience method to set the layout manually:

    function setLayout($layout) {
        $this->layout = $layout.self::EXT;
    }

And finally, we override the view method of loader:

    function view($view = '' , $view_data = array(), $return = FALSE) {  
        if ($view <> '') {
            $view = $view.self::EXT;
        }
        if ($this->layout <> '') {
            $view_data['content'] = parent::view($view, $view_data, TRUE);
            return parent::view($this->layout, $view_data, $return);
        } else {
            return parent::view($view, $view_data, $return);
        }
    }

If there is no layout, we use the standard process. Otherwise, we first merge the view and data to create partial content, and them inject that into the layout.

Remember the configuration? We add the following line to the end of ./application/config/config.php:

$config['layout'] = 'layouts/default';

Of course, we also have to create our default layout: views/layouts/default.phtml - I'll leave that up to you.

 You can find more about extending codeigniter at http://www.codeigniter.com/user_guide/general/core_classes.html

6 comments:

  1. This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted. Thank you so much for this article. keep update
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  2. thank you a lot for sharing this all-powerful weblog.Very inspiring and harmonious to steer too.wish you still allocation more of your ideas.i will definitely love to gate.! Virtual Audio Cable Crack

    ReplyDelete
  3. thanks for assisting human beings income the information they mannerism. brilliant stuff as pleasing. preserve up the massive take feature!!!! Tally ERP 9 Crack Version Download With GST

    ReplyDelete
  4. it became a first rate unintentional to visit this nice of site and i am satisfied to recognize. thank you therefore lots for giving us a chance to have this possibility..! 1st Birthday Wishes For Baby Boy

    ReplyDelete
  5. I like this blog so much because ts contain informative stuff.
    premium code zenmate

    ReplyDelete
  6. I was really impressed by this software. It gives me help in solving problems. that's nice.
    https://thepcsoft.com/nitro-pro-enterprise-portable/

    ReplyDelete