In CodeIgniter, you can create a common layout structure using a template file, which helps you manage the header, footer, and dynamic content for each page efficiently. This tutorial will guide you through setting up a basic template structure and using it within your CodeIgniter application.

 

File Structure

To implement this template system, you will create the following files:

applications/view/innerpages
	- footer.php
	- header.php
	- template.php

applications/controllers
	- Welcome.php

 

Step 1: Create template.php in application/views/innerpages/

The template.php file acts as the main layout template that loads the header, footer, and dynamic content based on the page being accessed.

File: application/views/innerpages/template.php

<?php 
	$this->load->view('innerpages/header.php');
	$this->load->view($main_content);
	$this->load->view('innerpages/footer.php');
?>

 

Explanation

  • The header.php and footer.php views are loaded statically.
  • $main_content is a dynamic variable that points to the specific view file that needs to be loaded for each page.

 

Step 2: Create header.php in application/views/innerpages/

The header.php file contains the common header elements of your pages, such as the <html> structure, the <head> section, and the opening <body> tag.

File: application/views/innerpages/header.php

 

<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>

 

Step 3: Create footer.php in application/views/innerpages/

The footer.php file contains the common footer elements of your pages, including the closing tags.

File: application/views/innerpages/footer.php

 

<h1><?php $heading; ?></h1>

</body>
</html>

 

Step 4: Create Welcome.php in application/controllers/

The Welcome.php controller is responsible for loading the view files and passing data to them.

File: application/controllers/Welcome.php

class Welcome extends CI_Controller {

	public function index() {
		$data = [];
		$data[‘title’] 		= ‘Page Title’;
		$data[‘heading’]		= ‘Welcome to infovistar’
		$data[‘main_content’]	= ‘about_us’;	// page name
		$this->load->view(‘innerpages/template’, $data);
	}
}

Explanation

  1. Loading the Template: The template.php view is loaded with the $data array, which contains the title, heading, and the main content to be displayed dynamically.
  2. Dynamic Content: The $main_content variable determines which view file to load dynamically, allowing you to switch the main content without altering the header and footer.

 

Step 5: Create the Main Content View (about_us.php)

Although not explicitly mentioned in the original instruction, you need a file named about_us.php to render the main content dynamically.

File: application/views/about_us.php

<h2>About Us</h2> 
<p>This is the About Us section of the website.</p>

 

Template Structure: We created a common layout using template.php to include the header, footer, and dynamic content.

Reusability: This approach enhances code reusability by isolating common elements (header and footer) and loading the main content dynamically.

Controller Integration: The Welcome controller demonstrates how to pass data to the template and specify which content to load.

 

You can effectively manage your application’s layout with a clean, reusable structure in CodeIgniter 3. If you need further customization or have any questions, feel free to ask!