PHPackages                             jtolj/html-attributes - 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. jtolj/html-attributes

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

jtolj/html-attributes
=====================

A Fluent Interface for Handling HTML Attributes in PHP.

v2.1.0(5y ago)186.6k↓11.2%[1 PRs](https://github.com/jtolj/html-attributes/pulls)MITPHPPHP &gt;=7.3

Since Aug 7Pushed 2y ago1 watchersCompare

[ Source](https://github.com/jtolj/html-attributes)[ Packagist](https://packagist.org/packages/jtolj/html-attributes)[ Docs](https://github.com/jtolj/html-attributes)[ RSS](/packages/jtolj-html-attributes/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (9)Used By (0)

This project has been archived. If you are using Laravel, see Illuminate\\View\\ComponentAttributeBag.
------------------------------------------------------------------------------------------------------

[](#this-project-has-been-archived-if-you-are-using-laravel-see-illuminateviewcomponentattributebag)

A Fluent Interface for Handling HTML Attributes in PHP
======================================================

[](#a-fluent-interface-for-handling-html-attributes-in-php)

The package provides a simple class, inspired by Drupal's Drupal\\Core\\Template\\Attribute, to help manage HTML attributes in a structured way.

I use it primarily in Laravel, so the pseudocode examples below use Laravel conventions. The package, however, is not specific to Laravel and can be used without it.

Examples
--------

[](#examples)

### In a Controller

[](#in-a-controller)

```
use App\Post;
use Jtolj\HtmlAttributes\HtmlAttributes;

$post = Post::find(1);
$post->is_wide = true;
$attributes = new HtmlAttributes();
$attributes
    ->addClass('card');
    ->setAttribute('id', "post-{$post->id}");
$attributes->addClassIf('card--wide', $post->is_wide);

echo "$post->escaped_content"
```

### Output

[](#output)

```
Hello World
```

### Using the example Trait with a Eloquent model and Blade template:

[](#using-the-example-trait-with-a-eloquent-model-and-blade-template)

```
namespace App;

use Illuminate\Database\Eloquent\Model;
use Jtolj\HtmlAttributes\Traits\HasHtmlAttributes;
class Post extends Model
{
    use HasHtmlAttributes;
}
```

```
@foreach ($posts as $post)
    htmlAttributes()->addClass('card')->addClassIf('even', $loop->even) !!}>
    {{ $post->summary }}

@endforeach
```

### Output

[](#output-1)

```
First Post
Second Post
Third Post
```

Escaping and Filtering
----------------------

[](#escaping-and-filtering)

Escaping of attribute names and values is done using the laminas/laminas-escaper package. Attribute keys are escaped using the [escapeHtmlAttr()](https://github.com/laminas/laminas-escaper/blob/2.7.x/src/Escaper.php#L158) method. As of 2.0, attribute values are escaped using the [escapeHtml()](https://github.com/laminas/laminas-escaper/blob/2.7.x/src/Escaper.php#L145) method.

Additionally, by default attribute names starting with 'on' (javascript event handlers) are not output.

You can set your own list of stripped prefixes with the `setUnsafePrefixes(array $prefixes)` method. Attribute names beginning with those prefixes are stripped on output.

You can also turn this behavior off by calling `allowUnsafe()`. This will not filter the list of attribute names before output and will output the value of 'unsafe' attributes fully unescaped (as of 2.0). **Be extremely careful with this behavior to prevent XSS.**

```
use Jtolj\HtmlAttributes\HtmlAttributes;

$attributes = new HtmlAttributes;
$attributes->addClass('card');
$attribute->setAttribute('onclick', 'alert("Hello";)');

$safe_string = (string) $attributes;
//class="card"

$attributes->allowUnsafe();
$unsafe_string = (string) $attributes;
//class="card" onclick="alert(\"Hello\";)"
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.3% 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 ~11 days

Total

6

Last Release

2056d ago

Major Versions

v1.0.3 → v2.02020-09-30

### Community

Maintainers

![](https://www.gravatar.com/avatar/4ec7e09e9e5d0e61deb31221a107a83612261e575f801c590e75f43d10595ea2?d=identicon)[jtolj](/maintainers/jtolj)

---

Top Contributors

[![jtolj](https://avatars.githubusercontent.com/u/845525?v=4)](https://github.com/jtolj "jtolj (14 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

templatingattributestemplatesHTML Attributesescape attributes

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jtolj-html-attributes/health.svg)

```
[![Health](https://phpackages.com/badges/jtolj-html-attributes/health.svg)](https://phpackages.com/packages/jtolj-html-attributes)
```

###  Alternatives

[twig/twig

Twig, the flexible, fast, and secure template language for PHP

8.4k443.2M5.8k](/packages/twig-twig)[league/plates

Plates, the native PHP template system that's fast, easy to use and easy to extend.

1.5k5.9M232](/packages/league-plates)[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[smarty/smarty

Smarty - the compiling PHP template engine

2.3k39.1M395](/packages/smarty-smarty)[laminas/laminas-view

Fast and type safe HTML templating library with a flexible plugin system supporting multistep template composition

7526.3M230](/packages/laminas-laminas-view)[shoot/shoot

Shoot aims to make providing data to your templates more manageable

40229.9k2](/packages/shoot-shoot)

PHPackages © 2026

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