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.
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.
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.
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); }
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>
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
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
thank for share! I feel very useful
very help full thanks brother