Php

Simple Example of CodeIgniter Custom Layout

In this tutorial I will describe how to create layout for CodeIgniter. We have twig library to get layout method but if you want define your templates variables like title, content area and website description, the you need to create you custom layout.

In CodeIgniter you need to do below things:

1- Create template file and put into lib folder
2- Include this library file in Code Igniter config file when the application load.
3- Create layout file as a master page for application
4- Call view in controller file.

Step 1:
First step we will create template class for layout. Before creating calls we need to think about requirement of layout, there are two thing one is content area and second one is template variable. Here we will take a template variable which will keep all template variables. Template is key value pair variable where key is the content area of template.

We will create class file:

class Template {
  var $template_data = array();
  
  function set($content_area, $value)
  {
   $this->template_data[$content_area] = $value;
  }
 
  function load($template = '', $name ='', $view = '' , $view_data = array(), $return = FALSE)
  {               
   $this->CI =& get_instance();
   
   $this->set($name , $this->CI->load->view($view, $view_data, TRUE));
   $this->CI->load->view('layouts/'.$template, $this->template_data);
  }
}

Above file put in CodeIgniter lib folder.

Related Post

Step 2: Include this file into application, so for this put below line in config/autoload.php file.
$autoload['libraries'] = array('template');

Step 3: Created template.php file and put in views/layouts folder:

<html>
<head>
    <title><?php echo $title ?></title>
    <link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<header id="header">
 <div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
   <div class="container">
    <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
           <span class="icon-bar"></span>
           <span class="icon-bar"></span>
           <span class="icon-bar"></span>
          </button>
          <a class="brand" href="/">CI Sample Project</a>
          <div class="nav-collapse collapse">
           <ul class="nav">
            <li class="active"><a href="/">Home</a></li>
            <li><a href="#">Link2</a></li>
            <li><a href="#">Link3</a></li>
            <li><a href="#">Link4</a></li>
            <li><a href="#">Link5</a></li>
           </ul>
          </div>
   </div>
  </div>
 </div>
</header>
<div style="padding-top:45px;height:540px">

    <div id="contents" style ="width:510px;float:left;border:1px solid green;"><?php echo $contents ?></div> 
 
</body>
</html>
>

Step 4:Now we will Create action method in controller to render view file and set all variable in template layout file.

/**
  * Index Page
  */ public function index()
 {
  $data = array();
  $this->template->set('title', 'Home');
  $this->template->load('template', 'contents' , 'home', $data);
 }

Here terms are:

Template : layout name
Contents : layoput Content area
Home : view file anme
Data : The parameters of file

Step 5: Created home.php file into views/ folder to view data.

<h4>Hello! I am Home Page</h4>

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories