[RFC] Helper and/or class to manipulate and print HTML element attributes

I have moved over from Drupal to Laminas, and a lot of my work continues to involve writing HTML templates. Drupal has a hugely useful Attribute class for manipulating and printing out the attributes of an HTML element: https://git.drupalcode.org/project/drupal/-/blob/9.0.x/core/lib/Drupal/Core/Template/Attribute.php

I really miss Attribute when working with laminas-view. It doesn’t look like laminas-view has anything similar?

If I were to write a laminas-view helper similar to Drupal’s Attribute, are there existing classes/design patterns I should be aware of that might be useful in its creation?

https://docs.laminas.dev/laminas-view/helpers/html-tag/ looks quite close. But the attributes manipulation should work with all elements, and preferably on its own without additional features.

Hello and welcome to the forums! :smiley:

I like the idea. :+1:

The helpers of laminas-view used the following method internally:

And laminas-form contains some important extensions for attributes:

Nothing special, you can follow the description in the documentation of view helpers.

But I think we need some better names because Drupal’s implementation uses an unbeautiful naming:

Attribute::setAttribute('…');
Attribute::hasAttribute('…');
Attribute::removeAttribute('…');
Attribute::addClass('…');

“Add a class to an attribute” looks wrong. :wink:

And the new helper should be used in all other helpers to manage HTML attributes.