Codeigniter Template Library

Codeigniter Template Library

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');<br />  /**
  * Template
  *
  * WordPress like template for CodeIgniter
  *
  * @package        Template
  * @version        0.1.0
  * @author        WebInOne
  * @link            http://www.webduos.com/codeigniter-template-library/
  * @copyright        Copyright (c) 2011, WebInOne
  * @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.

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');<br /> /**
  * Template
  *
  * WordPress like template for CodeIgniter
  *
  * @package        Template
  * @subpackage    Helpers
  * @version        0.1.0
  * @author        WebInOne
  * @link            http://www.webinone.net/codeigniter-template-library/
  * @copyright        Copyright (c) 2011, WebInOne
  * @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.

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


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

&lt;/div&gt;
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">
Design by <a href="http://webinone.net">Web In One</a></div>
<!-- /#footer -->

<!-- /#pagewrap -->

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>
This is content from index.</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'] = "WebInOne : 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.

<?php echo $title; ?>
<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.

Related Posts


15 thoughts on “Codeigniter Template Library

  1. ENukeX Crack

    It’s really a great and helpful piece of information. I am satisfied that you simply shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

    Reply
  2. autoayuda

    Hi, i feel that i noticed you visited my website thus i came to “go back the desire”.I am trying to to find things to improve my website!I suppose its adequate to use a few of your ideas!!

    Reply
  3. Blues Saxophone lessons

    I’m curious to find out what blog platform you are utilizing? I’m having some small security issues with my latest blog and I’d like to find something more secure. Do you have any recommendations?

    Reply
  4. TEWEDE

    This is very awesome article, Thank you very much.

    Please keep write article around codeigniter 🙂 we love it

    Reply
  5. Carol

    hey admin thanks for wonderful and effortless understandable put up i beloved your blog web page seriously significantly bookmarked also

    Reply
  6. Owen Knower

    I just want to mention I am all new to blogs and actually savored your page. Probably I’m want to bookmark your website . You surely have amazing well written articles. Thanks for revealing your blog site.

    Reply
  7. wyszukiwarka mp3

    Good information. Lucky me I found your blog by accident (stumbleupon). I have bookmarked it for later!

    Reply
  8. http://texascabletv.net/time-warner-cable-in-texas/time-warner-cable-in-austin-texas-78729/

    Heya are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any coding knowledge to make your own blog? Any help would be really appreciated!

    Reply
  9. comcast houston

    Thanks , I’ve recently been looking for information approximately this topic for a while and yours is the best I have came upon so far. But, what about the bottom line? Are you certain in regards to the source?

    Reply
  10. refluks

    I’m really enjoying the theme/design of your blog. Do you ever run into any internet browser compatibility issues? A small number of my blog audience have complained about my website not working correctly in Explorer but looks great in Firefox. Do you have any ideas to help fix this issue?

    Reply
  11. voordelig webdesign

    Heya i’m for the primary time here. I found this board and I find It really helpful & it helped me out a lot. I’m hoping to offer something back and aid others like you aided me.

    Reply
  12. Lyle Levans

    I simply want to tell you that I am just very new to blogging and certainly savored you’re web-site. Most likely I’m going to bookmark your blog . You absolutely have excellent articles and reviews. Cheers for sharing your website.

    Reply
  13. auto approve blog list

    You made some clear points there. I did a search on the topic and found most individuals will consent with your site.

    Reply
  14. Keneth Kazmer

    I just want to say I’m new to blogging and definitely enjoyed this page. More than likely I’m planning to bookmark your website . You absolutely have very good article content. Thank you for sharing your website.

    Reply

Leave a Reply

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


*