Creating Template In CodeIgniter 3 Using Bootstrap Theme

codeIgniter-theme

 

We will integrate bootstrap theme with CodeIgniter using layout. This tutorial help to create own custom layout for CodeIgniter. This is a master template which will use to render child view file, like each website template has header, footer and sidebar view which is constant and nothing has been changed between different pages.

We will define a master layout, which has constant things as its and variable things like inner content will change based on routing or request of page. First question is our mind what will be variable in CodeIgniter layout?, the page title, logo message, page content etc, we will use page title and content as a variable into this Codeigniter tutorial.

codeIgniter-theme

Pre-Requisites For CodeIgniter 3 Template Tutorial

There are following library and framework must be downloaded and configured

Example Of Codeigniter Templates Using Bootstrap Admin Theme

I am using bootstrap admin theme to create CodeIgniter template. You can change theme as per your application need. The CI templates integration process would be same like any other HTML theme.I am creating ci_test folder into xampp/htdocs/ directory and moved all files and directories from CodeIgniter 3 downloaded folder to xampp/htdocs/ci_test folder.

I Am Using Following Files And Folder

We will create and modify following CI files.

  • libraries/template.php : This file will use to create template class and method to render layout.
  • views/layouts/default_layout.php : This file will use to create HTML layout using template.php class.
  • config/config.php : This file will use configure application level params.
  • config/autoload.php : This file will use to load libraries on project instantiate.
  • config/router.php : This file will use to define route path of application.
  • views/home.php, views/about.php : home and about view file.
  • controllers/home.php : Default application controller file and use to render home,about view file using template.

Modify config.php file
We will modify some configuration parameters in application/config/config.php file, I made following changes into this file:

Added .htaccess file
We will add .htaccess file into route of /ci_test folder.We will write some rule for SEO and User-Friendly URLs. You can access employee list using this http://ci_test/employee url instead of http://ci_test/index.php/employee.

Modify autoload.php
We will modify autoload.php file for template library class.We will add helper library class in this file.You can find changes later on this tutorial.

Change Default Controller In Routes.Php File

Let’s change default controller name from welcome to home, whenever page will load into browser without the controller name, The default controller will be home and index() method will be call.

$route['default_controller'] = 'home';

How To Created Template Library In CI

Step 1: We will create template class for render template view file.Created a template.php file and stored into the /library folder.

Step 2: We will create a default_layout.php view file into the views/layouts/ folder.This file will contains all js, css libraries files with html inner container variable.

Step 3: Added template entry into config/autoload.php file to load template class when the CI has been initialize.

$autoload['libraries'] = array('template');
$autoload['helper'] = array('url');

Created Home Controller In CI

We have made change in config for default controller which was home, so we will create new php file Home.php file into the /controllers folder.Also created a index() method into it.Please add below code into Home.php controller file.

Create Home View File

We have defined home view file in index() controller method.We will create a new home.php file into views/ folder.Please add below code into this file,

Codeigniter Pages Using Template

We will add a new page into this codeigniter tutorial, So that you can understand the use of layout.Let’s create a about page which will render on the same page layout.We just send view to the Default Layout file and rest of theme structure same as home.
Step 1: Create a new entry route into route.php file.

We have mentioned home is controller and about is a method.

Step 2: Created about method into Home.php controller file.

Step 3: Created about.php view file into views folder.

Conclusion

This is a codeigniter beginners tutorial help to integrate beautiful bootstrap theme with Codeigniter using Layout.You can also integrate simple HTML theme using this codeigniter tutorial.We have create new controller and view file and render them using template.