Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 29 revisions

[h1]This is a cake-like layout/view system...[/h1]

[h2]The Class File: (/application/libraries/Layout.php)[/h2]

[code] <?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Layout {

var $obj;
var $layout;

function Layout($layout = "layout_main")
{
    $this->obj =& get_instance();
    $this->layout = $layout;
}

function setLayout($layout)
{
  $this->layout = $layout;
}

function view($view, $data=null, $return=false)
{
    $data['content_for_layout'] = $this->obj->load->view($view,$data,true);
    
    if($return)
    {
        $output = $this->obj->load->view($this->layout,$data, true);
        return $output;
    }
    else
    {
        $this->obj->load->view($this->layout,$data, false);
    }
}

} ?> [/code]

[h2]In Your Controller:[/h2]

Place this in the Controllers Constructor: [code] $this->load->library('layout', 'layout_main'); [/code]

or

Autoloading the library in your (/application/config/autoload.php) file: [code] $autoload['libraries'] = array('layout'); [/code]

Place this in the Controllers Methods: [code] $this->layout->view('/shop/view_cart', $data); [/code]

[h2]Master Layout Files: (/application/views/layout_main.php)[/h2]

Your View file must use $content_for_layout. For example:

[code] <html> <head> <title><?=$title_for_layout?></title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <link rel="stylesheet" href="/css/main.css" type="text/css" /> </head> <body>

<?=$content_for_layout?>
Footer
</body> </html> [/code]

Clone this wiki locally