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 aboveInstalling
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; } } } ?>
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); } } ?>
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');
$autoload['helper'] = array('template');
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 -->
./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();
get_header('primary'); get_footer('primary'); get_sidebar('left');
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');
You can also call with two parameter like below.
get_template_part('about','other');
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(); } } ?>
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.
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 .
ReplyDeleteReally Great Post
ReplyDeleteMany Thanks!!!
A great website with interesting and unique material what else would you need. quality wordpress themes
ReplyDeleteGreat Post
ReplyDeleteHow to implement multi domain support with single installation.
Thanks
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
ReplyDeleteSelenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune
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 |
ReplyDeleteThanks to the admin of this blog for sharing these kinds of useful information; Have been waiting for more updates.
ReplyDeleteEnglish Language School Franchise
Language Education Franchise
Spoken English Franchise In India
Computer Training Institute Franchise
Training Franchise Opportunities In India
Computer Education Franchise In India
Top Education Franchises
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.
ReplyDeleteoracle 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
Superb blog I visit this blog it's really awesome. The important thing is that in this blog content written clearly and understandable. The content of information is very informative.
ReplyDeleteOracle Fusion HCM Online Training
Oracle Fusion SCM Online Training
Oracle Fusion Financials Online Training
Big Data and Hadoop Training In Hyderabad
oracle fusion financials classroom training
Workday HCM Online Training
Oracle Fusion HCM Classroom Training
Workday HCM Online Training
Thanks for sharing
ReplyDelete"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
HVAC & Plumbing Services
ReplyDeleteAir 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
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.
ReplyDeleteI 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.
ReplyDeleteMLSU BA 1st Year Result 2022
MLSU BA 2nd Year Result 2022
MLSU BA 3rd Year Result 2022
This comment has been removed by the author.
ReplyDelete