PHPackages                             anvii/htmlelement - 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. anvii/htmlelement

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

anvii/htmlelement
=================

A library to create HTML code in PHP

1.1.2(1y ago)016LGPL-2.1PHPPHP &gt;=7.2.0

Since Sep 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/anvii/HtmlElement)[ Packagist](https://packagist.org/packages/anvii/htmlelement)[ RSS](/packages/anvii-htmlelement/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (5)Used By (0)

HtmlElement
===========

[](#htmlelement)

**HtmlElement provides an easy and efficient way to construct an HTML code in PHP**

HtmlElement is useful if you want to alternate you HTML code in PHP, like in modular architecture, and have elegant source code of your program.

Prerequisites
-------------

[](#prerequisites)

HtmlElement requires PHP v7.3 or greater

Building the HTML
-----------------

[](#building-the-html)

Sample code:

```
use anvii\HtmlElement\HtmlElement;

// Create an element
$div = new HtmlElement('div', ['class'=>'class1 class2'], 'Hello, World!');

// Build sample menu
$menu = HtmlElement::ul(['id'=>'sample-menu', 'class'=>'menu'], [
    HtmlElement::li(['class'=>'active'],
        HtmlElement::a(['href'=>'...'], 'New')
    ),
    HtmlElement::li(['class'=>''],
        HtmlElement::a(['href'=>'...'], 'Open')
    ),
    HtmlElement::li(['class'=>'disabled'],
        HtmlElement::a(['href'=>'...'], 'Save')
    )
]);

// Add one more element(s)
$menu->add([
    HtmlElement::hr(),
    HtmlElement::li(
        HtmlElement::a(['href'=>'...', '_if'=>$hasHelp], 'Help')
    )
]);

// Query elements like in jQuery
$menu->query('a')->addClass('awesome');
$menu->query('li.disabled a')->removeClass('awesome');

// Send menu to browser
echo $menu;
```

Quering elements
----------------

[](#quering-elements)

The library has a simple query engine like in jQuery. It allows to alternate HTML code by modules on the fly.

```
$form = $html->first('form#login');
$form->query('input[type="submit"]')->addClass('nice-button');
```

Class HtmlQuery also supports callback functions, but using strings with function names is forbidden due to security reason.

```
$form->query(function($e) {
    return ($e instanceof \my\Button::class);
});
```

Note, that querying in HtmlElement is very restricted. It supports only one class and only one attribute.

Special attributes
------------------

[](#special-attributes)

HTML attributes started with underscore '\_' are special. They are not rendered, but used for building document.

```
echo HtmlElement::a(['href'=>'/user/profile', '_if'=>isUserRegistered()], 'Profile');
```

The next special attributes are used by HtmlElement:

- \_if - Conditional element. Value of this attribute can be boolean or a function.
- \_raw - Do not encode content of an element.
- \_weight - Used to define order of elements. See 'Ordering elements' below.
- \_hideempty - Don't show element if it's content is empty.

Ordering elements
-----------------

[](#ordering-elements)

You can order elements by using special attribute '\_weight'. For example, you can construct menus dynamically from modules:

```
$userMenu->add([
    HtmlElement::a(['href'=>'/user/profile', '_weight'=>10], 'Profile'),
    HtmlElement::a(['href'=>'/user/password', '_weight'=>12], 'Change Password'),
    HtmlElement::a(['href'=>'/user/Logout', '_weight'=>100], 'Logout'),
]);

// From module:
HtmlElement::first('#userMenu')->add(
    HtmlElement::a(['href'=>'gallery', 'weight'=>30])
);
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 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 ~271 days

Total

4

Last Release

534d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9065e362e8998e6fd0ddea6e2f3debcfc18873745304097b352b0b345cf24eea?d=identicon)[Andrey Solovyev](/maintainers/Andrey%20Solovyev)

---

Top Contributors

[![anvii](https://avatars.githubusercontent.com/u/50912737?v=4)](https://github.com/anvii "anvii (3 commits)")

### Embed Badge

![Health badge](/badges/anvii-htmlelement/health.svg)

```
[![Health](https://phpackages.com/badges/anvii-htmlelement/health.svg)](https://phpackages.com/packages/anvii-htmlelement)
```

###  Alternatives

[kornrunner/secp256k1

Pure PHP secp256k1

37566.4k109](/packages/kornrunner-secp256k1)[liborm85/composer-vendor-cleaner

Composer Vendor Cleaner removes unnecessary development files and directories from vendor directory.

35342.7k1](/packages/liborm85-composer-vendor-cleaner)[makinacorpus/php-bloom

Bloom filter implementation

178.9k](/packages/makinacorpus-php-bloom)

PHPackages © 2026

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