Membuat Class HTML Helper Sendiri - Hallo sahabat Dev-Create, Pada Artikel yang anda baca kali ini dengan judul Membuat Class HTML Helper Sendiri, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel OOP, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Membuat Class HTML Helper Sendiri
link : Membuat Class HTML Helper Sendiri

Baca juga


Membuat Class HTML Helper Sendiri

Membuat Class HTML Helper Sendiri

kali ini saya akan share tutorial cara membuat class HTML Helper sendiri, fungsi dari class HTML Helper yaitu mempersingkat waktu ketika anda membuat sebuah tag HTML, disini saya hanya membuat beberapa function yang paling sering di pakai ketika membuat sebuah struktur web, anda bisa menambahkanya function sendiri jika function dalam tutorial ini kurang lengkap, ok langsung saja ke pembahasan, pertama buat projek dengan nama terserah anda kemudian salin kode dibawah ini dan simpan file dengan nama Html.php, atau anda bisa mendownload class Html tersebut disini
<?php
class Html {

 private $html;
 private $result;
 private $options;
 private $selected;
 private $attributes;

 public function head($type = null, $url = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  if ($type == 'style') {
   $this->result .= '<link href="'.$url.'" rel="stylesheet" type="text/css"';
   $this->result .= $this->get_attributes();
   $this->result .= '/>';
  } elseif ($type == 'script') {
   $this->result .= '<script type="text/javascript" src="'.$url.'"';
   $this->result .= $this->get_attributes();
   $this->result .= '></script>';
  }

  return $this->result;
 }

 public function bold($text = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<strong';
  $this->result .= $this->get_attributes();
  $this->result .= '>'.$text.'</strong>';

  return $this->result;
 }

 public function heading($level = 1, $text = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<h'.$level;
  $this->result .= $this->get_attributes();
  $this->result .= '>'.$text.'</h'.$level.'>';

  return $this->result;
 }

 public function link($url = null, $text = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<a href="'.$url.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '>'.$text.'</a>';

  return $this->result;
 }

 public function img($src = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<img src="'.$src.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '/>';

  return $this->result;
 }

 public function hr($count = 1) {
  $this->result = null;

  for($i=1; $i<=intval($count); $i++) {
   $this->result .= '<hr />';
  }

  return $this->result;
 }

 public function br($count = 1) {
  $this->result = null;

  for($i=1; $i<=intval($count); $i++) {
   $this->result .= '< br />';
  }

  return $this->result;
 }

 public function nbs($count = 1) {
  $this->result = null;

  for($i=1; $i<=intval($count); $i++) {
   $this->result .= '& nbsp;';
  }

  return $this->result;
 }

 public function form_open($action = null, $method = 'GET', $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<form action="'.$action.'" method="'.$method.'" enctype="multipart/form-data"';
  $this->result .= $this->get_attributes();
  $this->result .= '>';

  return $this->result;
 }

 public function form_close() {
  return '</form>';
 }

 public function hidden($name = null, $value = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="hidden" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '/>';

  return $this->result;
 }

 public function text($name = null, $value = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="text" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '/>';

  return $this->result;
 }

 public function textarea($name = null, $text = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<textarea name="'.$name.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '>'.$text.'</textarea>';

  return $this->result;
 }

 public function password($name = null, $value = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="password" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '/>';

  return $this->result;
 }

 public function number($name = null, $value = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="number" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '/>';

  return $this->result;
 }

 public function date($name = null, $value = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="date" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '/>';

  return $this->result;
 }

 public function time($name = null, $value = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="time" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '/>';

  return $this->result;
 }

 public function url($name = null, $value = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="url" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '/>';

  return $this->result;
 }

 public function file($name = null, $value = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="file" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '/>';

  return $this->result;
 }

 public function radio($name = null, $value = null, $text = null, $checked = false, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="radio" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();

  if ($checked) {
   $this->result .= ' checked="checked"';
  }

  $this->result .= '/> '.$text;

  return $this->result;
 }

 public function checkbox($name = null, $value = null, $text = null, $checked = false, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<input type="checkbox" name="'.$name.'" value="'.$value.'"';
  $this->result .= $this->get_attributes();

  if ($checked) {
   $this->result .= ' checked="checked"';
  }

  $this->result .= '/> '.$text;

  return $this->result;
 }

 public function select($name = null, $selected = null, $options = array(), $attributes = null) {
  $this->selected = $selected;
  $this->options = $options;
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<select name="'.$name.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '>';
  $this->result .= $this->get_options();
  $this->result .= '</select>';

  return $this->result;
 }

 public function button($type = 'submit', $text = null, $attributes = null) {
  $this->attributes = $attributes;

  $this->result = null;
  $this->result .= '<button type="'.$type.'"';
  $this->result .= $this->get_attributes();
  $this->result .= '>'.$text.'</button>';

  return $this->result;
 }

 private function get_options() {
  $this->html = null;

  foreach ($this->options as $key=>$val) {
   $this->html .= '<option value="'.$key.'"';
   $this->html .= $this->selected == $key ? ' selected="selected"' : '';
   $this->html .= '>';
   $this->html .= $val;
   $this->html .= '</option>';
  }

  return $this->html;
 }

 private function get_attributes() {
  $this->html = null;

  if(is_array($this->attributes)) {
   foreach ($this->attributes as $key=>$val) {
    $this->html .= ' '.$key.'="'.$val.'"';
   }
  } else {
   $this->html .= ' '.$this->attributes;
  }

  return $this->html;
 }
}
jika sudah, sekarang cara mengguanakan class HTML Helper
catatan untuk properti $attributes anda boleh mengkosongkannya, tergantung dari kebutuhan attribute yang akan anda gunakan
head($type, $url, $attributes)
Properti head
$type type string default null, pilihanya hanya style dan script
$url type string default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->head('style', 'contoh.css', array('media'=>'all'));
echo $html->head('style', 'contoh.css', 'media="all"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <link href="contoh.css" rel="stylesheet" type="text/css" media="all" />

echo $html->head('script', 'contoh.js');
// jika di cetak akan menghasilkan seperti ini 
// <script type="text/javascript" src="contoh.js"></script>

bold($text, $attributes)
Properti bold
$text type string default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->bold('Contoh bold', array('id'=>'text'));
echo $html->bold('Contoh bold', 'id="text"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <strong id="text">Contoh bold</strong>

heading($level, $text, $attributes)
Properti heading
$level type integer default 1, pilihanya hanya dari 1 sampai 6
$text type string default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->heading(2, 'Contoh heading', array('id'=>'text'));
echo $html->heading(2, 'Contoh heading', 'id="text"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <h2 id="text">Contoh heading</h2>

link($url, $text, $attributes)
Properti link
$url type string default null
$text type string default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->link('contoh.php', 'Contoh link', array('id'=>'text'));
echo $html->link('contoh.php', 'Contoh link', 'id="text"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <a href="contoh.php" id="text">Contoh link</a>

img($src, $attributes)
Properti img
$src type string default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->img('contoh.jpeg', array('width'=>30, 'height'=>30, 'alt'=>'Contoh jpeg'));
echo $html->img('contoh.jpeg', 'width="30" height="30" alt="Contoh jpeg"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <img src="contoh.jpeg" width="30" height="30" alt="Contoh jpeg"/>

hr($count)
Properti hr
$count type integer default 1, anda bisa memasukan berapapun angka, sesuai dengan kebutuhan anda
cara mengguankan dalam PHP
$html = new Html();

echo $html->hr(2);
// jika di cetak akan menampilkan hasil seperti ini 
// <hr /><hr />

br($count)
Properti br
$count type integer default 1, anda bisa memasukan berapapun angka, sesuai dengan kebutuhan anda
cara mengguankan dalam PHP
$html = new Html();

echo $html->br(2);
// jika di cetak akan menampilkan hasil seperti ini 
// < br />< br />

nbs($count)
Properti nbs
$count type integer default 1, anda bisa memasukan berapapun angka, sesuai dengan kebutuhan anda
cara mengguankan dalam PHP
$html = new Html();

echo $html->nbs(2);
// jika di cetak akan menampilkan hasil seperti ini 
// & nbsp;& nbsp;

form_open($action, $method, $attributes)
Properti form_open
$action type string default null
$method type string default GET, pilihanya hanya GET dan POST
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->form_open('index.php', 'POST', array('id'=>'form'));
echo $html->form_open('index.php', 'POST', 'id="form"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <form action="index.php" method="POST" enctype="multipart/form-data" id="form">

form_close()

cara mengguankan dalam PHP
$html = new Html();

echo $html->form_close();
// jika di cetak akan menampilkan hasil seperti ini
// </form>

hidden($name, $value, $attributes)
Properti hidden
$name type string or integer default null
$value type string or integer default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->hidden('contoh', null, array('id'=>'contoh'));
echo $html->hidden('contoh', null, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="hidden" name="contoh" value="" id="contoh"/>

text($name, $value, $attributes)
Properti text
$name type string or integer default null
$value type string or integer default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->text('contoh', null, array('id'=>'contoh'));
echo $html->text('contoh', null, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="text" name="contoh" value="" id="contoh"/>

textarea($name, $text, $attributes)
Properti textarea
$name type string or integer default null
$text type string or integer default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->textarea('contoh', null, array('id'=>'contoh'));
echo $html->textarea('contoh', null, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <textarea name="contoh" id="contoh"></textarea>

password($name, $value, $attributes)
Properti password
$name type string or integer default null
$value type string or integer default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->password('contoh', null, array('id'=>'contoh'));
echo $html->password('contoh', null, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="password" name="contoh" value="" id="contoh"/>

number($name, $value, $attributes)
Properti number
$name type string or integer default null
$value type integer default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->number('contoh', null, array('id'=>'contoh'));
echo $html->number('contoh', null, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="number" name="contoh" value="" id="contoh"/>

date($name, $value, $attributes)
Properti date
$name type string or integer default null
$value type string or integer default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->date('contoh', null, array('id'=>'contoh'));
echo $html->date('contoh', null, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="date" name="contoh" value="" id="contoh"/>

time($name, $value, $attributes)
Properti time
$name type string or integer default null
$value type string or integer default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->time('contoh', null, array('id'=>'contoh'));
echo $html->time('contoh', null, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="time" name="contoh" value="" id="contoh"/>

url($name, $value, $attributes)
Properti url
$name type string or integer default null
$value type string default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->url('contoh', null, array('id'=>'contoh'));
echo $html->url('contoh', null, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="url" name="contoh" value="" id="contoh"/>

file($name, $value, $attributes)
Properti file
$name type string or integer default null
$value type string default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->file('contoh', null, array('id'=>'contoh'));
echo $html->file('contoh', null, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="file" name="contoh" value="" id="contoh"/>

radio($name, $value, $text, $checked, $attributes)
Properti radio
$name type string or integer default null
$value type string or integer default null
$text type string or integer default null
$checked type boolean default false
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->radio('contoh', null, 'Mobil', false, array('id'=>'contoh'));
echo $html->radio('contoh', null, 'Mobil', false, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="radio" name="contoh" value="" id="contoh"/> Mobil

echo $html->radio('contoh', null, 'Mobil', true, array('id'=>'contoh'));
echo $html->radio('contoh', null, 'Mobil', true, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="radio" name="contoh" value="" id="contoh" checked="checked"/> Mobil

checkbox($name, $value, $text, $checked, $attributes)
Properti checkbox
$name type string or integer default null
$value type string or integer default null
$text type string or integer default null
$checked type boolean default false
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->checkbox('contoh', null, 'Mobil', false, array('id'=>'contoh'));
echo $html->checkbox('contoh', null, 'Mobil', false, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="checkbox" name="contoh" value="" id="contoh"/> Mobil

echo $html->checkbox('contoh', null, 'Mobil', true, array('id'=>'contoh'));
echo $html->checkbox('contoh', null, 'Mobil', true, 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <input type="checkbox" name="contoh" value="" id="contoh" checked="checked"/> Mobil

select($name, $selected, $options, $attributes)
Properti checkbox
$name type string or integer default null
$selected type string or integer default null
$options type array default array()
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->select('contoh', null, array('S'=>'Kecil', 'M'=>'Sedang'), array('id'=>'contoh'));
echo $html->select('contoh', null, array('S'=>'Kecil', 'M'=>'Sedang'), 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <select name="contoh" id="contoh"><option value="S">Kecil</option><option value="M">Sedang</option></select>

echo $html->select('contoh', 'M', array('S'=>'Kecil', 'M'=>'Sedang'), array('id'=>'contoh'));
echo $html->select('contoh', 'M', array('S'=>'Kecil', 'M'=>'Sedang'), 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <select name="contoh" id="contoh"><option value="S">Kecil</option><option value="M" selected="selected">Sedang</option></select>

button($type, $text, $attributes)
Properti checkbox
$type type string default submit, pilihanya hanya submit, button dan reset
$text type string or integer default null
$attributes type string or array default null
cara mengguankan dalam PHP
$html = new Html();

echo $html->button('button', 'Contoh button', array('id'=>'contoh'));
echo $html->button('button', 'Contoh button', 'id="contoh"');
// jika di cetak akan menampilkan hasil yang sama yaitu seperti ini 
// <button type="button" id="contoh">Contoh button</button>
Ok, sampai disini dulu tutorial kali ini, selamat mencoba dan semoga bisa bermanfaat.


Demikianlah Artikel Membuat Class HTML Helper Sendiri

Sekianlah artikel Membuat Class HTML Helper Sendiri kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Membuat Class HTML Helper Sendiri dengan alamat link https://dev-create.blogspot.com/2019/07/membuat-class-html-helper-sendiri.html