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:
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
Step 4: Create a file “footer.php” under “application/views/layout/” for Footer section and add mentioned code
Step 5: Create a file “left.php” under “application/views/layout/” for Left section and add mentioned code
Step 6: Create front.php under “application/views/layout/” and add mentioned code
Step 7: Create a file “home.php” under “application/views/” for main body content
Step 8: Create a controller “Home.php” under “application/controllers/” and add below code :
PHP
Finish!! Your basic layout for codeigniter has been ready for use.
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 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>
<div style="clear:both;"></div>
<div style="height:50px; width:100%; border:1px solid #000;"> Your Website Footer Section</div>
<div style="clear:both;"></div>
<div style="height:500px; width:200px; border:2px solid #000; float:left;"> Left Area</div>
<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>
<div style="height:500px; width:70%; border:2px solid #000; float:left;"> Middle Area</div>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
public function index() {
$this->middle = 'home'; // its your view name, change for as per requirement.
$this->layout();
}
}
No comments:
Post a Comment