Back to Deck
[utility]
PHP HTML DOM Builder
SOURCE PHP
VERSION 1.0
AUTHOR Cododel
A complete HTML builder library for PHP that provides a fluent, object-oriented interface for constructing HTML elements. Originally created for building complex tables with modular architecture.
Features
- Fluent API: Chain methods for clean, readable code
- Type-safe: Classes for specific elements (Table, Row, Cell)
- Modular: Extend with custom modules (like TailwindModule)
- Static factory:
make()method for convenient instantiation - Attributes & Classes: Easy manipulation of HTML attributes and CSS classes
- Nested children: Full support for hierarchical structures
Use Case
Perfect for generating complex HTML structures in PHP, especially tables with financial reports, data grids, or admin panels:
$table = Table::make() ->class('text-center') ->children([ Row::make([ Cell::make("Financial result for activities related to contract #") ->class('font-bold') ->width(200) ->colSpan(2), Cell::make($contract_number), Cell::make("from"), Cell::make($contract_date), ]), Row::make([ Cell::make("Parameters: IGK")->colSpan(2), Cell::make($igk), ]), Row::make([ Cell::make("Indicator")->span(2, 2), Cell::make("Revenue")->rowSpan(2), Cell::make("Expenses")->colSpan(3), Cell::make("Profit (+) / Loss (-)")->rowSpan(2), ]), ]);
echo $table->render();Architecture
- HTMLElement: Base abstract class with rendering logic
- Table/Row/Cell: Specific implementations for table structures
- Module system: Plugin architecture for custom functionality
- Factory pattern: Static
make()for clean instantiation
Source Code
HTMLElement.php (Base class)
<?php
namespace App\Services\HTML;
use App\Services\HTML\Modules\Module;
abstract class HTMLElement{ protected static $modules = []; protected $attributes = []; protected $classList = []; protected $children = [];
public function __call($method, $args) { foreach (self::$modules as $module) { $callback = $module::getCallback($this);
if (is_callable($callback)) { return call_user_func($callback, ...$args); } } return $this; }
abstract protected static function getTagName(): string;
public function attr(string $name, $value) { $this->attributes[$name] = $value; return $this; }
public function getAttributes(): array { return $this->attributes; }
public function render() { $attributes = ''; foreach ($this->attributes as $name => $value) { $attributes .= " $name=\"$value\""; } if (!empty($this->classList)) { $attributes .= ' class="' . implode(' ', $this->classList) . '"'; }
$html = "<{$this->getTagName()} $attributes>";
foreach ($this->children as $child) { if ($child instanceof HTMLElement) $html .= $child->render(); else $html .= (string) $child; }
$html .= "</{$this->getTagName()}>";
return $html; }
public function children(array|HTMLElement|string|int $children): static { if (is_array($children)) { foreach ($children as $child) { $this->children($child); } } else { $this->children[] = $children; }
return $this; }
public function class(string $class): static { $this->classList[] = $class; return $this; }
public function getInnerText(): string { $text = ''; foreach ($this->children as $child) { if ($child instanceof HTMLElement) $text .= $child->getInnerText(); else $text .= $child; } return $text; }
public function getInnerHTML(): string { $html = ''; foreach ($this->children as $child) { if ($child instanceof HTMLElement) $html .= $child->render(); else $html .= $child; } return $html; }
public function getOuterHTML(): string { return $this->render(); }
public static function registerModule(string $module) { $moduleName = $module::getModuleName();
if (!property_exists(static::class, $moduleName)) { static::$modules[$moduleName] = $module; } else { throw new \Exception("Module name {$module::getModuleName()} is not allowed, it is a reserved name."); } }
public static function getModules(): array { return static::$modules; }
public static function make(array|string|int|null $children = []): static { $element = new static(); if (!empty($children)) { $element->children($children); } return $element; }}Table.php
<?php
namespace App\Services\Table;
use App\Services\HTML\HTMLElement;
class Table extends HTMLElement{ private $data;
public static function getTagName(): string { return 'table'; }
public function getValue(int $column, int $row) { return $this->data[$row][$column] ?? null; }}Row.php
<?php
namespace App\Services\Table;
use App\Services\HTML\HTMLElement;
class Row extends HTMLElement{ public static function getTagName(): string { return 'tr'; }}Cell.php
<?php
namespace App\Services\Table;
use App\Services\HTML\HTMLElement;
class Cell extends HTMLElement{ public static function getTagName(): string { return 'td'; }
public function rowSpan(int $rowSpan): static { $this->attr('rowspan', $rowSpan); return $this; }
public function colSpan(int $colSpan): static { $this->attr('colspan', $colSpan); return $this; }
public function span(int $rowSpan = 1, int $colSpan = 1): static { return $this->rowSpan($rowSpan)->colSpan($colSpan); }
public function width(int $width): static { $this->attr('width', $width); return $this; }
public function height(int $height): static { $this->attr('height', $height); return $this; }}