Belajar CSS

CSS Advantages from Quackit.com

CodeIgniter : Membuat library?

Logo CodeIgniter
Library pada framework CodeIgniter sangat bermanfaat ketika kita ingin membuat sebuah prosedur atau proses sendiri dalam program kita dengan tetap menggunakan Object Oriented, berbeda dengan helper yang hanya merupakan kumpulan fungsi-fungsi tanpa dibangun dalam sebuah class.

Berikut contoh penggunaan sederhana dari library untuk membuat sebuah desain template yang dapat digunakan di semua halaman sehingga mempermudah kita ketika kita ingin mengganti sebagian saja dari desain kita tanpa perlu mengaturnya kembali pada setiap halaman.

libraries/Template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template{
 
  var $CI = null;
 
  function __construct(){
  $this->CI =& get_instance();
 }
 
    public function get_header($page_title){
  $data['page_title'] = $page_title;
  $this->CI->load->view('header', $data);
    }
 
 public function get_sidebar(){
  $this->CI->load->view('sidebar');
 }
 
 public function get_footer(){
  $this->CI->load->view('footer');
 }
}
?>
views/header.php
1
2
3
4
5
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-weight: bold; font-style: inherit; font-family: inherit; vertical-align: baseline; "><?php</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(177, 177, 0); ">echo</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 136); ">$page_title</span> <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-weight: bold; font-style: inherit; font-family: inherit; vertical-align: baseline; ">?></span>

views/sidebar.php
1
2
3
<div id="sidebar">
    ini adalah sidebar
div>
views/content.php
1
2
3
<div id="content">
    ini adalah bagian content
div>
views/footer.php
1
2
3
4
5
<div id="footer">
    ini adalah bagian footer
div>
body>
html>
dan yang terakhir bagian Controller. controller/home.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 class Home extends CI_Controller{
 
  function __construct(){
  parent::__construct();
   $this->load->library('template');
 }
 
 public function index(){
  $page_title = "Site Title";
 
  $this->template->get_header($page_title);
  $this->load->view('content');
  $this->template->get_sidebar();
  $this->template->get_footer();
 }
}
?>

sumber: babibu.eamca.com

0 comments:

facebook

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes

KOMPUTER| TEMPAT BERBAGI TENTANG PERANGKAT LUNAK, APLIKASI DAN INFORMASI ...