CodeIgniter Forums
Layout Class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: Layout Class (/thread-51494.html)



Layout Class - El Forum - 05-06-2012

[eluser]John Nickell[/eluser]
Greetings all,

I am still fairly new to CodeIgniter, but I love what I've been able to do with it so far.

Hopefully my first CodeIgniter library can help someone else, as well.

I know that CodeIgniter already has the ability to load view partials. Also, after looking at some of the templating engines out there, I decided that I wanted to stick with PHP. I was still hoping for some furthur structure/separation.

This library was inspired by:
1. The NetTuts+ tutorial about creating a Layout Manager:
http://net.tutsplus.com/tutorials/php/how-to-create-a-layout-manager-with-codeigniter-new-premium-tutorial/
2. ASP.NET Master Pages
3. My previous experiences with templating solutions

The syntax I was looking for amounted to this:

Controller:
Code:
<?php
class home extends CI_Controller {
    public function index()
    {
        $this->layout->setTitle('My Site', 'Welcome');
        $this->layout->appendHeadLink('css/style.css');
        $this->layout->view('home', $this->data);
    }
}

Layout:
Code:
<?php print $this->layout->html('doctype'); ?>
<html>
<head>
<title><?php print $this->layout->html('title'); ?></title>
<?php print $this->layout->html('headlink'); ?>
</head>
<body>
<?php print $this->layout->section('header'); ?>
<?php print $this->layout->section('content'); ?>
<?php print $this->layout->section('footer'); ?>
<?php print $this->layout->html('footscript'); ?>
</body>
</html>

The final result is slightly different, but works very well for me so far.

If anyone here might get some use out of it, check it out here:
https://github.com/johnnickell/Layout-Class

Please let me know if you would have done it differently.

Any feedback positive or negative is appreciated.

Thanks!

John Nickell