Pages

Codeigniter Template Library for Wordpress Fun

Thursday, May 24, 2012
I am not only a Codeigniter fun but also Wordpress fun. So I try to crate a CI template library that look like Wordpress theme.

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  0.1.0
  * @author  TutsforWeb <http://tutsforweb.blogspot.comt>
  * @link   
  * @copyright  Copyright (c) 2011, TutsforWeb
  * @license  http://opensource.org/licenses/mit-license.php MIT Licensed
  * 
  */

  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;
    }
   }
  }
?>
(or)
Download from below button and place that file to './application/libraries/' directory.
Download Source Code

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

./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  0.1.0
  * @author  TutsforWeb <http://tutsforweb.blogspot.com>
  * @link   
  * @copyright  Copyright (c) 2011, TutsforWeb
  * @license  http://opensource.org/licenses/mit-license.php MIT Licensed
  *
  */
  
  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);
   }
  }
?>
(or)
Download from below button and place that file to './application/helpers/' directory.
Download Source Code

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>Design by <a href="http://tutsforweb.blogspot.com">Web In One</a></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'] = "TutsforWeb: Web design and developmetn tutorials";
  $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 -->

If you have any other tips, techniques or requirement, let us know in the comments.

14 comments:

  1. CodeIgniter is really a cool framework that provides you rich set of libraries for commonly needed tasks, it really minimizes your work as well as reduces your time by minimizing the code needed for the particular task .

    ReplyDelete
  2. Really Great Post

    Many Thanks!!!

    ReplyDelete
  3. A great website with interesting and unique material what else would you need. quality wordpress themes

    ReplyDelete
  4. Great Post
    How to implement multi domain support with single installation.
    Thanks

    ReplyDelete
  5. Excellent blog, I wish to share your post with my folks circle. It’s really helped me a lot, so keep sharing post like this
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

    ReplyDelete
  6. Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays. Well written article Thank You for Sharing with Us pmp training in chennai | pmp training class in chennai | pmp training near me | pmp training courses online | pmp training fee | project management training certification | project management training in chennai | project management certification online |

    ReplyDelete
  7. Superb blog I visit this blog it's extremely marvelous. Interestingly, in this blog content composed plainly and justifiable. The substance of data is exceptionally instructive.
    oracle fusion financials classroom training
    Workday HCM Online Training
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training
    Oracle Fusion HCM Classroom Training

    ReplyDelete
  8. Thanks for sharing
    "Pressure Vessel Design Course is one of the courses offered by Sanjary Academy in Hyderabad. We have offer professional
    Engineering Course like Piping Design Course,QA / QC Course,document Controller course,pressure Vessel Design Course,
    Welding Inspector Course, Quality Management Course, #Safety officer course."
    Piping Design Course
    Piping Design Course in India­
    Piping Design Course in Hyderabad
    QA / QC Course
    QA / QC Course in india
    QA / QC Course in Hyderabad
    Document Controller course
    Pressure Vessel Design Course
    Welding Inspector Course
    Quality Management Course
    Quality Management Course in india
    Safety officer course

    ReplyDelete
  9. HVAC & Plumbing Services
    Air Star Heating guarantees reliability and quality for all equipment and services.

    Air Star Heating specialists always try to deliver the most excellent quality of services to our customers at an affordable price. It is understood that every client has different needs and different problems. We try to accomplish the needs of every client according to their requests. We are having considerable experience in this field. Our specialists understand very well how things work. It doesn’t matter in which field of industry you are looking for services.
    Plumbing & HVAC Services in San Diego. Call now (858) 900-9977 ✓Licensed & Insured ✓Certified Experts ✓Same Day Appointment ✓Original Parts Only ✓Warranty On Every Job.
    Visit:- https://airstarheating.com

    ReplyDelete
  10. Thank you for this amazing post.. Wish to travel to Azerbaijan for business and tourism purpose then apply for azerbaijan visas online. That offers you fast and very secure process.

    ReplyDelete
  11. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.

    MLSU BA 1st Year Result 2022
    MLSU BA 2nd Year Result 2022
    MLSU BA 3rd Year Result 2022

    ReplyDelete