Kotchasan Framework Documentation
Html
Html
Kotchasan\Html is for programmatically creating HTML elements, supporting complex HTML structures.
Creating HTML
Creating Generic Elements
use Kotchasan\Html;
// Create div with Attributes
$div = Html::create('div', [
'id' => 'container',
'class' => 'wrapper'
]);
// Add Child Elements
$div->add('h1', ['class' => 'title'], 'Hello World');
$div->add('p', [], 'This is a paragraph');
echo $div->render();Creating Forms
$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 (via add method)
Html::add() supports special tags to invoke specific functions:
groups / groups-table
Create a group of Checkboxes or Radios for multiple selection.
$form->add('groups', [
'label' => 'Interests',
'checkbox' => [
'coding' => 'Coding',
'reading' => 'Reading'
],
'value' => ['coding'] // Selected values
]);inputgroups
Create a group of Inputs where rows can be added/removed (Dynamic Rows).
$form->add('inputgroups', [
'label' => 'Phones',
'id' => 'phones',
'value' => ['0812345678', '0898765432'],
'options' => ['Mobile', 'Home', 'Work'] // Optional datalist
]);radiogroups / checkboxgroups
Create a basic group of Radios or Checkboxes.
$form->add('radiogroups', [
'label' => 'Gender',
'name' => 'gender',
'options' => ['m' => 'Male', 'f' => 'Female'],
'value' => 'm'
]);ckeditor
Create CKEditor (Requires Kotchasan\CKEditor class).
$form->add('ckeditor', [
'id' => 'content',
'value' => '<p>Initial content</p>'
]);row / rowgroup
Create a div with class row or rowgroup for 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
Create a new HTML Element.
- If
$tagmatches a method inHtmlorForm, that method is called. - Otherwise, a new
HtmlObject is created.
form(array $attributes = []): static
Create a Form Element with Ajax and GForm support.
- Special Attributes:
ajax,gform,token,onsubmit,onbeforesubmit
fieldset(array $attributes = []): static
Create a Fieldset Element.
- Special Attributes:
title(for Legend),titleClass
Manipulation Methods
add(string $tag, array $attributes = []): static
Add a Child Element to the current Element.
- Supports Special Tags:
groups,groups-table,inputgroups,radiogroups,checkboxgroups,menubutton,ckeditor,row,rowgroup - Supports standard Tags and all Input Types from
Kotchasan\Form
appendChild(string|object $html): void
Append HTML String or Object directly to the current content.
script(string $script): void
Add JavaScript to the Element (or main Form if exists).
innerHtml(string $html): string
Generate HTML String of the Element with inner content (Stateless).
render(): string
Convert Object and all Child Elements to HTML String.
Property Access
$attributes (public array)
Access and modify Attributes directly.
$div->attributes['class'] = 'new-class';
$div->attributes['data-id'] = '123';