Web Template Tanpa Table - PHP, JavaScript, dan CSS (Bagian 3) Merubah Waktu / Jam dengan PHP
Nov 22

Berikut ini adalah engine yang akan digunakan membaca file template, mengisinya dengan modul-modul dan menampilkan ke web browser.

engine.php

PHP:
  1. class Template {
  2. var $html = "";
  3.  
  4. function Template($fileTemplate) {
  5. $namaFile = "$fileTemplate";
  6. $penunjukFile = fopen($namaFile, "r");
  7. $this->html = fread($penunjukFile, filesize($namaFile));
  8. fclose($penunjukFile);
  9. }
  10.  
  11. function isiVar($namaVar, $isiVar) {
  12. $this->html = ereg_replace("#$namaVar#", $isiVar, $this->html);
  13. }
  14.  
  15. function isiBlokKiri($modulnya = array()) {
  16. $isiBlokTotal = "";
  17. foreach($modulnya as $kunci => $nilai) {
  18. include_once("kiri/$nilai.php");
  19. $isiBlokTotal .= $dt;
  20. }
  21. $this->html = ereg_replace("#BLOCK_KIRI#", $isiBlokTotal, $this->html);
  22. }
  23.  
  24. function isiContent($modulenya) {
  25. include_once("content/$modulenya.php");
  26. $this->html = ereg_replace("#CONTENT#", $dt, $this->html);
  27. }
  28.  
  29. function isiBlokKanan($modulnya = array()) {
  30. $isiBlokTotal = "";
  31. foreach($modulnya as $kunci => $nilai) {
  32. include_once("kanan/$nilai.php");
  33. $isiBlokTotal .= $dt;
  34. }
  35. $this->html = ereg_replace("#BLOCK_KANAN#", $isiBlokTotal, $this->html);
  36. }
  37.  
  38. function tampilkanContent() {
  39. echo $this->html;
  40. }
  41. }

index.php

PHP:
  1. error_reporting("E_ERROR");
  2.  
  3. include_once("engine.php");
  4.  
  5. $tpl = new Template('utama.htm');
  6.  
  7. $content = $_REQUEST['content'];
  8. if (empty($content)) {
  9. $content = 'home';
  10. }
  11.  
  12. $kiri = array('menu-vertikal', 'kotak-weblogin');
  13. $kanan = array('polling', 'shoutbox');
  14.  
  15. $tpl->isiVar('JUDUL_WEBSITE', 'Website Buat Belajar Aja');
  16.  
  17. $tpl->isiBlokKiri($kiri);
  18. $tpl->isiContent($content);
  19. $tpl->isiBlokKanan($kanan);
  20.  
  21. $tpl->tampilkanContent();

Silakan di cek lagi susunan file dan direktori adalah sebagai berikut:

index.php
utama.htm
table.css
engine.php
kiri/menu-vertikal.php
kiri/kotak-weblogin.php
content/home.php
content/berita-terbaru.php
kanan/polling.php
kanan/shoutbox.php

Selamat mencoba!

Leave a Reply

You must be logged in to post a comment.