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.

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>

2 thoughts on “Simple Example of CodeIgniter Custom Layout

Leave a Reply

Your email address will not be published. Required fields are marked *