Codeigniter – A Master Layout

In this screencast, we’ll explore a few different techniques to DRY up your codeigniter views by implementing a master layout file for your site. Here’s a quick start if you’d like to setup your own master model.

<!DOCTYPE html>
<html>
  <head>
    <title><?php echo isset($title) ? "PHP Sandbox: $title" : NULL; ?></title>
    <link rel="stylesheet" href="/assets/css/site.css" type="text/css" />
  </head>
  <body>
    <?php $this->load->view('layout/navigation'); ?>
    <div class="container">
      <?php $this->load->view($content); ?>
    </div>
    <script type="text/javascript" src="/assets/js/site.js"></script>
  </body>
</html>

Assuming you’ve configured your default layout within core/MY_Controller.php

<?php
class MY_Controller extends CI_Controller {

  public $layout;

  public function __construct()
  {
    parent::__construct();
    $this->layout = 'layout/master';
  }
}

… you would call the master layout from your views like this:

$data['title'] = $product->name;
$data['content'] = 'products/show';
$this->load->view($this->layout, $data);
Advertisements

4 thoughts on “Codeigniter – A Master Layout

  1. I enjoyed watching your codeigniter screencasts.
    Very informative. Thank you. Lookin’ forward to #3 🙂
    Would you be so kind to share the source code used in the tuts?
    To fully understand I would like a closer look.

    thx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s