PHPackages                             stefan-wiebe/elements - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. stefan-wiebe/elements

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

stefan-wiebe/elements
=====================

A simple PHP-library for building HTML-Elements.

1.0.2.1(8y ago)09MITPHP

Since Feb 3Pushed 8y ago1 watchersCompare

[ Source](https://github.com/stefanwiebe/elements)[ Packagist](https://packagist.org/packages/stefan-wiebe/elements)[ RSS](/packages/stefan-wiebe-elements/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (6)Used By (0)

Elements
========

[](#elements)

This is a simple library to construct HTML elements with PHP, mainly to avoid getting into a concatenation-hell.

1 Generating an element
-----------------------

[](#1-generating-an-element)

### 1.1 Constructor

[](#11-constructor)

The first way to generate an Element is using its constructor.

It takes the following parameters

ParameterTypeDescriptionExampleDefault$tagStringThe HTML-tag`'div'`Required$attributesarrayHTML-attributes`['id' => 'wrap-god']``array()`$contentarrayThe element's contents`['hi', $span]``array()`You can put everything into an Element's contents as long as it parses to a String, including other Elements.

```
use Elements\Element;

$attributes = [
	'id' => 'wrap-god',
	'class' => ['fresh', 'mexican', 'tortilla'],
];

$div = new Element('div', $attributes);
```

Classes can be passed as Array (as shown here) or using a String, separating them with spaces (`'fresh mexican tortilla'`). Note however that Strings will be exploded. As in the PHP-function.

### 1.2 Factory Method

[](#12-factory-method)

There's also a fairly simple factory method for generating an Element only with a tag and the provided classes. As mentioned before, you can either provide an Array of classes or seperate them by spaces.

ParameterTypeDescriptionExampleDefault$tagStringThe HTML-tag`'span'`Required$classesArray | StringThe classes`'winged-hussar'`Required```
use Elements\Element;

$div = Element::withClasses('div', 'undetected unexpected');
```

2. Modifying Attributes
-----------------------

[](#2-modifying-attributes)

Since Elements implement [ArrayAccess](http://php.net/manual/de/class.arrayaccess.php), its attributes can be manipulated by accessing the Element like an Array.

Let's say we wanted to give an id to the Element created in 1.2. We would do it like this:

```
$div['id'] = 'night-witch';
```

With the exception of classes and styles, all attributes are Strings.

### 2.1 Classes

[](#21-classes)

Classes are internally handled as Arrays, as previously mentioned. This enables you to easily add and remove classes without having to worry about having one space too many or too few.

If you wanted to add some to our pre-existing div, you could do it like so:

```
$div['class'][] = 'wings-of-glory';
$div['class'][] = 'tell-the-story';
```

Replacing them works in the same way as declaring them in the first place:

```
$div['class'] = ['deviation', 'aviation'];
// or
$div['class'] = 'deviation aviation';
```

### 2.2 Styles

[](#22-styles)

Styles are also handled as Arrays, however unlike classes, they are associative Arrays, since it'd be kind of hard otherwise.

You can add classes like so:

```
$div['style']['display'] = 'none'; // Stealth perfected
```

Note that you can also pass a String to set an Element's styles, but it's not recommended since I can't guarantee that the regex will work 100% of the time. I didn't find any glaring issues, but I also didn't test a lot.

```
$div['style'] = 'display: none; background: linear-gradient(to bottom, red, blue);'
```

3 Content
---------

[](#3-content)

An Element's contents can be anything that parses to a String. They are kept as array and can be manipulated using the `addContent($content)` and `setContent(array $content)` methods.

Example:

```
$div = Element::withClasses('div', 'form-group');

$input = new Element('input');
$input['type'] = 'text';

$div->addContent($input);
```

4 Outputting Elements
=====================

[](#4-outputting-elements)

In order to output an Element, simply echo it or cast it to a string if you need to.

```
echo $div;

$someString .= (string) $div;
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

3017d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c658af9fda31c78960866d04dfd88d7e4ff83deabbc5113302a3405d6ee31c9d?d=identicon)[Stefan Wiebe](/maintainers/Stefan%20Wiebe)

---

Top Contributors

[![stefan-wiebe](https://avatars.githubusercontent.com/u/13772046?v=4)](https://github.com/stefan-wiebe "stefan-wiebe (1 commits)")

---

Tags

htmldomhtml elements

### Embed Badge

![Health badge](/badges/stefan-wiebe-elements/health.svg)

```
[![Health](https://phpackages.com/badges/stefan-wiebe-elements/health.svg)](https://phpackages.com/packages/stefan-wiebe-elements)
```

###  Alternatives

[wa72/htmlpagedom

jQuery-inspired DOM manipulation extension for Symfony's Crawler

3383.9M34](/packages/wa72-htmlpagedom)[caxy/php-htmldiff

A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.

21520.9M15](/packages/caxy-php-htmldiff)[mkalkbrenner/php-htmldiff-advanced

An add-on for the php-htmldiff library for comparing two HTML files/snippets and highlighting the differences using simple HTML.

3517.8M1](/packages/mkalkbrenner-php-htmldiff-advanced)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
