Codeigniter Template Library for WordPress Fun

I have got this library and it will be used with the helper together.

By using this library, you can crate the themes like in WordPress.

Requirement

Codeigniter 2.0.0 and above

Installing

Copy and paste below code in your new document and save as

./application/libraries/Template.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  /**
  * Template
  * 
  * WordPress like template for CodeIgniter
  * 
  * @package  Template
  * @version  1.0
  * 
  */

  class Template
  {
   private $ci;
   private $tp_name;
   private $data = array();

   public function __construct() {
    $this->ci = &get_instance();
   }

   public function set($name='') {
    $this->tp_name = $name;
   }

   public function load($name = 'index') {
    $this->load_file($name);
   }

   public function get_header($name) {
    if(isset($name)) {
     $file_name = "header-{$name}.php";
     $this->load_file($file_name);
    }
    else {
     $this->load_file('header');
    }
   }

   public function get_sidebar($name) {
    if(isset($name)) {
     $file_name = "sidebar-{$name}.php";
     $this->load_file($file_name);
    }
    else {
     $this->load_file('sidebar');
    }
   }

   public function get_footer($name) {
    if(isset($name)) {
     $file_name = "footer-{$name}.php";
     $this->load_file($file_name);
    }
    else {
     $this->load_file('footer');
    }
   }

   public function get_template_part($slug, $name) {
    if(isset($name)) {
     $file_name = "{$slug}-{$name}.php";
     $this->load_file($file_name);
    }
    else{
     $this->load_file($slug);
    }
   }

   public function load_file($name)
   {
    if($this->get_data($name))
    {
     $data = $this->get_data($name);
     $this->ci->load->view($this->tp_name.'/'.$name,$data);
    }
    else {
     $this->ci->load->view($this->tp_name.'/'.$name);
    }
   }

   public function set_data($key, $data) {
    $this->data[$key] = $data;
   }

   public function get_data($key) {
    if(isset($this->data[$key])) {
     return $this->data[$key];
    }
    else {
     return false;
    }
   }
  }
?>

./application/helpers/template_helper.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 /**
  * Template
  *
  * WordPress like template for CodeIgniter
  *
  * @package  Template
  * @subpackage Helpers
  * @version  1.0
  *
  */
  
  if ( ! function_exists('get_header'))
  {
   function get_header($name=null)
   {
    $ci =& get_instance();
    return $ci->template->get_header($name);
   }
  }

  if ( ! function_exists('get_sidebar'))
  {
   function get_sidebar($name=null)
   {
    $ci =& get_instance();
    return $ci->template->get_sidebar($name);
   }
  }

  if ( ! function_exists('get_footer'))
  {
   function get_footer($name=null)
   {
    $ci =& get_instance();
    return $ci->template->get_footer($name);
   }
  }

  if ( ! function_exists('get_template_part'))
  {
   function get_template_part($slug, $name=null)
   {
    $ci =& get_instance();
    return $ci->template->get_template_part($slug, $name);
   }
  }
?>

Loading Template

Then open your autoload.php from your ‘./application/config/’ directory and set

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

and

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

(or) you can load Template just like any other library and helper in your controller using the $this->load->library function:

$this->load->library('template');
$this->load->helper('template');

Usage

Creating our theme
Now we can start to write our theme. Create a folder in your views folder named default. Let’s start with header.php. Copy and paste the following code in you new document and save as header.php in your default theme folder like below.

./application/views/default/header.php

<html>
<head>
<title>Home</title>
</head>
<body>
<div id="pagewrap">
 <div id="header">
  <h1>Your Site Name</h1>
 </div><!-- /#header -->

Next one is footer. Copy and paste the following code in you new document and save as footer.php in your default theme folder like below.

./application/views/default/footer.php

 <div id="footer">
  <p>Your footer code here</p>
 </div><!-- /#footer --> 

</div><!-- /#pagewrap -->
</body>
</html>

Next one is sidebar. Copy and paste the following code in you new document and save as footer.php in your default theme folder like below.

./application/views/default/sidebar.php

<div id="sidebar">
 <h2>This is side bar.</h2>
</div><!-- /#sidebar -->

Before we do not write index.php, I want to introduce you some function.

get_header();
get_footer();
get_sidebar();

If you call above function in your view file, it will load your header.php, footer.php and sidebar.php respectively from your theme folder. And then you can also call with the parameter like below.

get_header('primary');
get_footer('primary');
get_sidebar('left');

If you call like above, it will load your header-primary.php, footer-primary.php and sidebar-left.php respectively look like in WordPress.

Now let’s start our index file. Copy and paste the following code in you new document and save as index.php in your default theme folder like below.

./application/views/default/index.php

<?php get_header(); ?>
<div id="content">
 <h2>Content</h2>
  <p>This is content from index.</p>
</div><!--<div id="content">-->
<?php
get_sidebar(); 
get_footer();
?>

The last function for your view file is get_template_part(). It can call any other view file by writing like below.

get_template_part('about');

The above function will load the about.php from your theme folder.

You can also call with two parameter like below.

get_template_part('about','other');

The above function will load the about-other.php from your theme folder.

Controller

$this->template->set()
Firstly you need to set the theme name in your controller.
Assuming that you have tow themes like below.

./application/views/default/index.php
./application/views/default/header.php
./application/views/default/footer.php
./application/views/default/sidebar.php
./application/views/default/about.php
and
./application/views/newtheme/index.php
./application/views/newtheme/header.php
./application/views/newtheme/footer.php
./application/views/newtheme/sidebar.php
./application/views/newtheme/project.php

Now you have two themes named default and newtheme.
In your controller you need to set a theme you want to use like below.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller{
 public function __construct() {
  parent::__construct();
 }
 
    public function index() {
  $this->template->set('default');
 }
}
?>

$this->template->load()
Now you can load your theme.
If you load with no parameter, it will load index.php like below.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller{
 public function __construct() {
  parent::__construct();
 }
 
    public function index() {
  $this->template->set('default');
         $this->template->load();
 }
}
?>

If you load with the parameter, it will load the view file that you set as parameter as below.

$this->template->set('default');
$this->template->load('about');

$this->template->set_data()

If you want to pass your data from controller to view, you must use this function. Let’s discuss how to use this function in your controller and in your view file.

Now we want to pass our title from our controller to view file. Below is controller syntax:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller{
 public function __construct(){
  parent::__construct();
 }

 public function index() {
  $this->template->set('default');
  $header_data['title'] = "Web Title";
  $this->template->set_data('header',$header_data);
  $this->template->load();
 }
}
?>

First parameter is your view file name.
You can you in your header.php view file as you used in CI view file like below.

<html>
<head>
 <title><?php echo $title; ?></title>
</head>
<body>
<div id="pagewrap">
 <div id="header">
  <h1>Your Site Name</h1>
 </div><!-- /#header -->