How to Create a Layout Manager with CodeIgniter: New Premium Tutorial
plusvideos

How to Create a Layout Manager with CodeIgniter: New Premium Tutorial

Tutorial Details
  • Topic: CodeIgniter, Controllers, PHP
  • Difficulty: Moderate
  • Format: Screencast and Written
Download Source Files

This Premium video, and companion article will teach you how to create a simple, yet powerful library to handle layouts in the popular CodeIgniter framework. The library you’ll create will allow you to maximize your efficiency, save time and code, modularize your views and even your Javascript and CSS files.

The basic functionality of this library will be very simple. We’ll take the contents of a view, render them with the appropriate data, then take the rendered content and assign it to a variable. Now, we’ll render the layout itself, and replace a part of the layout with the contents of this variable. Simple, but powerful enough.

The idea is to mimic the calls to $this->load->view(). When we call this method, we pass the name (and location) of our view, and then an array of data that will be accessible from the view. Here’s an example:

function method($url_param)
{
  $this->load->view('controller_views/method_view', array('url_param' => $url_param));
}

The above code will take the file system/application/views/controller_views/method_view.php, pass it the url_param variable, and then send it to the browser. Here’s where we come in. We will not send the content to the browser yet. Instead, we’ll send it to the layout, then to the browser. But how do we do that?

…Become a Premium member to follow along with this screencast/article and find out!


Join Net Premium

NETTUTS+ Screencasts and Bonus Tutorials

For those unfamiliar, the family of Tuts+ sites runs a premium membership service. For $19 per month, you gain access to exclusive premium tutorials, screencasts, and freebies from Nettuts+, Psdtuts+, Aetuts+, Audiotuts+, Vectortuts+, and CgTuts+ For the price of a pizza, you’ll learn from some of the best minds in the business. Become a Premium member to read this tutorial, as well as hundreds of other advanced tutorials and screencasts.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.sz-media.org Sz-Media

    Premium… and you need a creditcard on paypal to get it…. hate that!

    • lugger

      lol…

      why pay for that when you can red it at the FREE User Guide

  • Adrian

    Thanks, I’ve been looking for a layout manager similar to CakePHP. This is perfect.

  • Nathan II

    This here has been enough reason for me to have purchased a premium membership! Awesome stuff! When I saw Code Igniter didn’t have this by default I wasn’t keen on using it. So now that I see how simple it really is to make my own! I am stoked! Code Igniter rocks! Awesome tut by the way! Now if only we could make this into a cms with layout selector! Think that’d be a great tut as well!

    • Adrian

      Its really simple all you need to do it add a function which queries a Database or Config file for the picked theme then prepend the includes with the theme folder and such

      public function current_theme() {
      return ‘themes/’.$this->CI->config->item(‘theme’).’/';
      }

      this example just gets the theme from a config file, the prepending portion would just have the call to this function before the base_url()

      $this->includes[] = base_url() . $this->current_theme() . $path;

      Hopefully this helps

      • http://ian.linkworks.cl Ian Murray
        Author

        That looks right. Great and simple ;)

      • Nathan II

        Cool! Now I need to get working on this LOL! :) I am currently building my own CMS and I am trying to nail down not only a framework but a language I can use as well. I prefer Coldfusion but I know PHP as well and from what I see of CodeIgniter I must say its AWESOME!

        Thanks dude!

  • http://www.kotretan.com imamiscool

    Hate That!
    Simply because we don’t have a premium membership we can’t get that tutorial anymore. Regardless it is screencast nor article..

    ps. pardon my english. I just hate when “money” limits the way to share information. :(

    • Brad

      I Dont mind paying back for the amount of info I have gotten from this site over the past couple years or so.. Premium membership is well worth it.

      Just doing my part to make sure Jeffery Way gets good vacations, and sticks around to give more good stuff, and find good writers like Burak and Ian. :)

    • Sid

      Your english has got nothing to do with your sense of entitlement. This site provides plenty of free content for you. It is ads and premium content which pays for the free stuff. Do you realise that?

  • Paul du Long

    Finally some new CI tuts!

  • Filip Kostovski

    Well i have done something like that extending the default Controller and adding a render method, something like ruby on rails does. I have created on github my personal codeigniter startup (just search for codeigniter_startup) which have also included render json methods firephp logging etc. Anyway i really like the codeigniter series on nettuts, you are doing a great job. Keep it up.

  • Brian Ray

    Wonderful idea. I’ve always passed a $data array to the view to do most of this functionality. Here’s an example:

    function index()
    {
    $data = array(
    ‘title’ => ‘Home’,
    ‘content’ => ‘content/home’,
    );
    $this->load->view(‘template/home.php’, $data);
    }

    Then in the home.php view, load the content with:

    load->view($content); ?>

  • http://Lavishimage.com Imamu Hunter

    I’m having issues with unziping this file. Can you guys help me.

  • Carlos

    That’s it, I’m renewing my Tutsplus account only for the last 2 Codeigniter Premium tutorials :)

  • Guilherme Ventura

    Awesome!, But all the tutorials CI are in video format, no possibility to publish tutorials in text format as well?

    Great Regards!

  • arnold

    Cool ,

  • Denis Bazhan (db)

    Nice tuts! More tuts on CI.

  • http://ian.linkworks.cl Ian Murray
    Author

    Another thing I like to do with this library is to create an “empty” layout (one that only echoes the $content_for_layout), and use it for ajax calls. This way, if you enable compression in the config file, you don’t get errors you would get by using simple echos in these calls.

    Be sure to follow me on twitter :D (I’m @Synt4x there).

    Cheers!

  • Eddy

    Could someone please email me the source files???
    thanks so much!!!!
    mynameis@eddyf.co.uk

  • http://www.tkp.com.br/ Thiago

    Awesome!, But all the tutorials CI are in video format, no possibility to publish tutorials in text format as well?

    Great Regards!

    • Guilherme Ventura

      You could at least have exchanged a few words before copying and pasting my comment my friend :)

  • http://www.incisivepoint.com Jack Starr

    60% hate that, why you’re not share it…it’s good more than money…

    cheap.

  • http://sig.ly/ Nico

    I just do it through hooks and I pass the current CI’s output (the loaded view from the controller) to a Document Library which I wrote, which then builds the headers and the rest of the markup required for a full display of the page.

    Simple.

  • Jon-Erik

    Totally great screencast, thou your voice sounded like you were whispering.

    First thing that popped on my mind that will you cover themes but as screencast ended it was supa easy to extend it myself, just throw and extra private variable that hold theme folder which is set where ever you like, prolly best in db and use function to get it and call it from construct, well thats everyones own choise!

    Keep these CI Tuts coming matey!

  • http://mixmo-anime.blogspot.com kankuro

    Cool CI tuts..

  • Premium

    $9.00 USD for each month
    the premium is due every 1st of the month or is it no matter when you are premium member and it is one of the day on which the account was created???

  • http://spotdex.com David

    The idea is actually pretty interesting, since ever since now I have been including all of my headers, footers, ect… manually. What is super error prone, because I always forget…

  • Tom Van Assche

    Just went premium because of this tutorial, keep it up.

  • http://www.ilyassalmanfilmleri.org İlyas Salman

    Awesome! =)

  • wburningham

    What is the app you used to create the new files? It seemed like you have a command line interface because you used the touch command.

  • http://www.net2011.com hbnu001

    Is‘t it a video tutorials?How to download it,and I just see a text tutorials.I am a student from china and my English is bad.

    • Alexander

      you have to pay.

  • sadasdasd

    dasdasdasd