Friday 11 September 2015

How To Create Layouts In Codeigniter 3.0

Codeigniter 3.0 does not have any layouts feature by default. We have to write some basic php code for enabling layout concept in codeigniter. In this tutorial we will explain how to create custom layout or template for CodeIgniter 3.0. In the below steps we assumes the page have 4 sections e.g header, footer, left panel and body area.
Follow the mentioned steps to create layouts or templates in codeigniter 3.0:

Step 1: Create a “MY_Controller.php” under “application/core/” and add below code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class MY_Controller extends CI_Controller
    {
        //set the class variable.
        public $template  = array();
        public $data      = array();
        /*Loading the default libraries, helper, language */
        public function __construct(){
            parent::__construct();
            $this->load->helper(array('form','language','url'));
            $this->lang->load('english');
        }
        /*Front Page Layout*/
        public function layout() {
            // making template and send data to view.
            $this->template['header']   = $this->load->view('layout/header', $this->data, true);
            $this->template['leftpanel']   = $this->load->view('layout/leftpanel', $this->data, true);
            $this->template['body'] = $this->load->view($this->body, $this->data, true);
            $this->template['footer'] = $this->load->view('layout/footer', $this->data, true);
            $this->load->view('layout/front', $this->template);
        }
    }
Step  2: Create a folder “layout” under “application/views/”.

Step 3: Create a file “header.php” under “application/views/layout/” for header section and add mentioned code

<div style="height:100px; width:100%; border:1px solid #000;"> Your Website Header Section </div>

Step  4: Create a file “footer.php” under “application/views/layout/” for Footer section and add mentioned code
Step  6: Create front.php under “application/views/layout/” and add mentioned code

<html>
    <head>
        <title>StepBlogging.COM Layouts</title>
    </head>
    <body>
    <?php
        if($header) echo $header ;
        if($left)   echo $left ;
        if($middle) echo $middle ;
        if($footer) echo $footer ;
    ?>
    </body>
</html>


Step 7: Create a file “home.php” under “application/views/” for main body content

<div style="height:500px; width:70%; border:2px solid #000; float:left;"> Middle Area</div>
Step  8: Create a controller “Home.php” under “application/controllers/” and add below code :PHP

No comments:

Post a Comment