Kotchasan Framework Documentation

Kotchasan Framework Documentation

Html

TH 05 Feb 2026 07:52

Html

\Kotchasan\Html สำหรับสร้าง HTML elements แบบ Programmatic ที่รองรับการสร้างโครงสร้าง HTML ที่ซับซ้อน

การสร้าง HTML

การสร้าง Element ทั่วไป

use Kotchasan\Html;

// สร้าง div พร้อม Attributes
$div = Html::create('div', [
    'id' => 'container',
    'class' => 'wrapper'
]);

// เพิ่ม Child Elements
$div->add('h1', ['class' => 'title'], 'Hello World');
$div->add('p', [], 'This is a paragraph');

echo $div->render();

การสร้าง Form

$form = Html::form([
    'id' => 'myForm',
    'class' => 'form-horizontal',
    'action' => 'save.php',
    'method' => 'post',
    'onsubmit' => 'return validateForm()'
]);

$fieldset = $form->add('fieldset', [
    'title' => 'User Info'
]);

$fieldset->add('text', [
    'id' => 'username',
    'label' => 'Username'
]);

echo $form->render();

Advanced Elements (ผ่านเมธอด add)

Html::add() รองรับ tag พิเศษเพื่อเรียกใช้ฟังก์ชันเฉพาะ:

groups / groups-table

สร้างกลุ่มของ Checkbox หรือ Radio สำหรับเลือกได้หลายค่า

$form->add('groups', [
    'label' => 'Interests',
    'checkbox' => [
        'coding' => 'Coding',
        'reading' => 'Reading'
    ],
    'value' => ['coding'] // Selected values
]);

inputgroups

สร้างกลุ่ม Input ที่สามารถเพิ่ม/ลบ แถวได้ (Dynamic Rows)

$form->add('inputgroups', [
    'label' => 'Phones',
    'id' => 'phones',
    'value' => ['0812345678', '0898765432'],
    'options' => ['Mobile', 'Home', 'Work'] // Optional datalist
]);

radiogroups / checkboxgroups

สร้างกลุ่ม Radio หรือ Checkbox แบบพื้นฐาน

$form->add('radiogroups', [
    'label' => 'Gender',
    'name' => 'gender',
    'options' => ['m' => 'Male', 'f' => 'Female'],
    'value' => 'm'
]);

ckeditor

สร้าง CKEditor (ต้องมีคลาส Kotchasan\CKEditor)

$form->add('ckeditor', [
    'id' => 'content',
    'value' => '<p>Initial content</p>'
]);

row / rowgroup

สร้าง div ที่มีคลาส row หรือ rowgroup สำหรับจัด Layout

$row = $form->add('row');
$row->add('text', ['label' => 'Col 1', 'class' => 'width50']);
$row->add('text', ['label' => 'Col 2', 'class' => 'width50']);

อ้างอิงเมธอด (Method Reference)

Static Creation Methods

create(string $tag, array $attributes = []): static

สร้าง HTML Element ใหม่

  • หาก $tag ตรงกับเมธอดใน Html หรือ Form จะเรียกใช้เมธอดนั้น
  • มิฉะนั้นจะสร้าง Object Html ใหม่

form(array $attributes = []): static

สร้าง Form Element พร้อมรองรับ Ajax และ GForm

  • Attributes พิเศษ: ajax, gform, token, onsubmit, onbeforesubmit

fieldset(array $attributes = []): static

สร้าง Fieldset Element

  • Attributes พิเศษ: title (สำหรับ Legend), titleClass

Manipulation Methods

add(string $tag, array $attributes = []): static

เพิ่ม Child Element เข้าไปใน Element ปัจจุบัน

  • รองรับ Tags พิเศษ: groups, groups-table, inputgroups, radiogroups, checkboxgroups, menubutton, ckeditor, row, rowgroup
  • รองรับ Tags ปกติและ Input Types ทั้งหมดจาก Kotchasan\Form

appendChild(string|object $html): void

เพิ่ม HTML String หรือ Object ต่อท้ายเนื้อหาปัจจุบันโดยตรง

script(string $script): void

เพิ่ม JavaScript เข้าไปใน Element (หรือ Form หลักถ้ามี)

innerHtml(string $html): string

สร้าง HTML String ของ Element พร้อมเนื้อหาภายใน (ไม่เก็บ State)

render(): string

แปลง Object และ Child Elements ทั้งหมดเป็น HTML String

Property Access

$attributes (public array)

เข้าถึงและแก้ไข Attributes โดยตรง

$div->attributes['class'] = 'new-class';
$div->attributes['data-id'] = '123';

คลาสที่เกี่ยวข้อง

  • Form - สำหรับ Input Types ต่างๆ ที่เรียกผ่าน $html->add() ได้
  • Input - การจัดการค่า Input