PHPackages                             northrook/html-element - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. northrook/html-element

ActiveLibrary[Templating &amp; Views](/categories/templating)

northrook/html-element
======================

A collection of classes for generating HTML elements and their attributes.

0133PHP

Since Dec 23Pushed 1y agoCompare

[ Source](https://github.com/northrook/html-element)[ Packagist](https://packagist.org/packages/northrook/html-element)[ RSS](/packages/northrook-html-element/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

HTML Elements
=============

[](#html-elements)

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](https://www.php.net/manual/en/class.domdocument.php).

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
------------

[](#installation)

```
composer require northrook/html-element
```

Usage
-----

[](#usage)

New elements can be created using the `Element` class:

```
namespace Northrook\HTML\Element;

$basic = new Element( content: 'Hello World!' );

echo $basic;
```

```
Hello World!
```

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 );
```

```

    Hello World!

```

```
$button = new Element(
    tag        : 'button',
    attributes : [ 'id' => 'Save Action', 'class' => 'btn icon' ],
    content    : [
        new Element( 'i', content: '
