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

Category:Library::View | Category:Library::Community

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

[h3]Source[/h3][b]/application/libraries/Layout.php[/b] [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)
{
    $loadedData = array();
    $loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true);
    
    if($return)
    {
        $output = $this->obj->load->view($this->layout, $loadedData, true);
        return $output;
    }
    else
    {
        $this->obj->load->view($this->layout, $loadedData, false);
    }
}

} ?> [/code] [h3]Usage[/h3] [h4]Loading the library[/h4] You can use it in two ways. First and obvious is to place the following line in the controller's constructor function. [code]$this->load->library('layout', 'layout_main');[/code] The second is by enabling codeIgniter to autoload the library by editing [b]/application/config/autoload.php[/b][code]$autoload['libraries'] = array('layout');[/code]After you do either of the above, and the library is loaded, in your controller you may do this: [code]$this->layout->view('/shop/view_cart', $data);[/code] [h4]Master layout[/h2] [b]/application/views/layout_main.php[/b] 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