northrook / html-element
A collection of classes for generating HTML elements and their attributes.
Requires
- php: >=8.3
- northrook/support: dev-main
Requires (Dev)
- northrook/dev-env: dev-main
- phpstan/phpstan: @stable
- voku/portable-ascii: ^2.0
This package is auto-updated.
Last update: 2024-12-19 12:22:51 UTC
README
Generate HTML elements and their attributes.
The motivation behind this package is to provide a simple way to generate HTML elements with sensible defaults, while being more light-weight than the DOMDocument class.
Elements can be nested to create complex HTML structures, but unlike DOMDocument it does not provide a way fully traverse the DOM tree.
This is a deliberate design decision to keep the package lightweight and performant.
Important
This package is still in development.
While it is considered MVP and stable, it may still undergo breaking changes.
Note
Documentation is still being written.
Installation
composer require northrook/html-element
Usage
New elements can be created using the Element
class:
namespace Northrook\HTML\Element; $basic = new Element( content: 'Hello World!' ); echo $basic;
<div>Hello World!</div>
Elements can be nested using the $content
parameter.
It accepts strings, Elements, and arrays of either.
Important
The Element
class does not escape provided $content
, so ensure you do so either before passing it, or later down the line.
echo new Element( 'h1', [ 'class' => 'example classes' ], $basic );
<h1 class="example classes"> <div>Hello World!</div> </h1>
$button = new Element( tag : 'button', attributes : [ 'id' => 'Save Action', 'class' => 'btn icon' ], content : [ new Element( 'i', content: '<svg ... </svg>' ), 'Save', ] ); echo $button;
<button id="save-action" type="button" class="btn icon"> <i>...</i> Save </button>