Codeigniter Create Template Layout

Codeigniter is very light zero configuration  powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.

Today we are going to config template layout with codeigniter. i’m using Admin-lte html template for the project.

first we need to download codeigniter and extract our project folder (inside wampp/xampp folder ).

now we need to partial our html file as header.php, sidebar.php, content.php, footer.php

for my consideration i’m using prefix for partial files. now put files into layout folder inside view folder.

now we create our master layout file as master_layout.php

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<?php $this->load->view('templates/layout_header'); ?>
<?php $this->load->view('templates/layout_sidebar'); ?>
<?php $this->load->view('templates/layout_navbar'); ?>
<?php echo $the_view_content; ?>
<?php $this->load->view('templates/layout_footer') ?>

now we have to create our base controller for our template files. we need to put controller file inside application/core/MY_Controller.php codeigniter will automatically detect this controller file by the prefix MY_. we can change the configuration prefix in config.php as $config['subclass_prefix'] = 'MY_';
now we going our newly created MY_Controller.php file and code like this

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct(); $this->data['app_name'] = 'App Name';
$this->data['page_title'] = 'CI App';
$this->data['page_description'] = 'Codeigniter Layout Developing';
}

protected function render($the_view = NULL, $template = 'layout_master')
{
if($template == 'json' || $this->input->is_ajax_request())
{
header('Content-Type: application/json');
echo json_encode($this->data);
}
elseif(is_null($template))
{
$this->load->view($the_view,$this->data);
}
else
{
$this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view, $this->data, TRUE);
$this->load->view('templates/' . $template . '_view', $this->data);
}
}
}

Inside the construct we can use global app variables as we want.

now our app controllers we can define with fed codes like this. as example i’m created customer.php controller

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Customer extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->model('Customer_model');
}

public function index() {
$this->data['page_title'] = 'Customer Data';
$this->render('customer');
}
}

by kushan


avatar
  Subscribe  
Notify of