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

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

oscarotero/html
===============

HTML generator

v1.0.0(1y ago)153942MITPHPPHP &gt;=7.2CI failing

Since Apr 27Pushed 1y ago2 watchersCompare

[ Source](https://github.com/oscarotero/html)[ Packagist](https://packagist.org/packages/oscarotero/html)[ Docs](https://github.com/oscarotero/html)[ RSS](/packages/oscarotero-html/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (7)Dependencies (3)Versions (8)Used By (0)

html
====

[](#html)

PHP library to generate HTML code

Install
-------

[](#install)

Note, PHP ~7.2 is required

```
composer require oscarotero/html
```

Usage
-----

[](#usage)

```
namespace Html;

//Create a div
$div = div();

//Render the div
echo $div;
//

//Create a div with text content
echo div('Hello world');
//Hello world

//Create a div with more text content
echo div('Hello', ' world');
//Hello world

//HTML entities are escaped
echo div('Hello', ' ');
//Hello &lt;world&gt;

//Use the function raw() to do not escape html entities
echo div('Hello', raw(' '));
//Hello

//Create a div with html content
echo div('Hello ', strong('world'));
//Hello world

//A div with many content
echo div(
    h1('Hello world'),
    p('This is a paragraph'),
    ul(
        li('And this is a list item'),
        li('Other list item')
    )
);
```

Attributes
----------

[](#attributes)

There are two ways to add attributes to the html tags: using an array as the first argument or using magic methods.

```
//Add attributes using an array
echo div(['id' => 'foo'], 'Hello world');

//Or add attributes with magic methods
echo div('Hello world')->id('foo');

//Both examples outputs: Hello world
```

All attributes with `null` as value are omitted:

```
//Add attributes using an array
echo div(['id' => null], 'Hello world');

//Or add attributes with magic methods
echo div('Hello world')->id(null);

//Both examples outputs: Hello world
```

### Flags

[](#flags)

In HTML, flags (or boolean attributes) are attributes that does not need a value. Use `boolean` values to add flags.

Example using array syntax:

```
//Positive flag
echo div(['hidden' => true]);
//

//Negative flag
echo div(['hidden' => false]);
//

//A short method to add positive flags:
echo div(['hidden']);
//

//Mixing attributes and flags
echo div(['hidden', 'class' => 'foo']);
//
```

Example using magic methods:

```
//Positive flag
echo div()->hidden(true);
//

//Negative flag
echo div()->hidden(false);
//

//A short method to add positive flags (true is not needed):
echo div()->hidden();
//
```

### Classes

[](#classes)

Some attributes can contain several space-separated values, for example `class`. You can use an array to add classes:

```
//Using an array
echo div(['class' => ['foo', 'bar']]);

//Using the magic method:
echo div()->class(['foo', 'bar']);

//Both examples return:
```

Use the `key => value` syntax to add classes under some conditions. Only if the value is evaluated as true, the class will be added:

```
//Using an array
echo div([
    'class' => [
        'foo',
        'bar',
        'theme-blue' => true,
        'error' => !empty($error)
    ]
]);

//Using the magic method:
echo div()->class([
    'foo',
    'bar',
    'theme-blue' => true,
    'error' => !empty($error)
]);

//Both examples output:
```

### data-\* attributes

[](#data--attributes)

Any `data-*` attribute containing a non-scalable value, will be converted to json. Unlike flags, boolean values are included too:

```
//Using an array
echo div([
    'data-enabled' => true,
    'data-other' => false,
    'data-omitted' => null, //Null values are ommitted
    'data-options' => ['foo', 'bar']
]);

//Using the special method `data()`
echo div()
    ->data('enabled', true),
    ->data('other', false),
    ->data('omitted', null),
    ->data('options', ['foo', 'bar']);

//Both examples output:
```

Import/Export
-------------

[](#importexport)

Elements implements `JsonSerializable` and `Serializable` interfaces, so you can export them:

```
$article = article(
    header(
        h1('Hello world')
    ),
    div(['class' => 'content'],
        p('This is an article')
    )
);

//Export to JSON
$json = json_encode($article);

//Use the function array2Html to recreate the html from json:
$article = array2Html(json_decode($json, true));

//Serialize and unserialize
$ser = serialize($article);
$article = unserialize($ser);
```

Other interfaces implemented
----------------------------

[](#other-interfaces-implemented)

All elements that can contain children (not self-closing elements like `` or ``) implement the following standard PHP interfaces:

### ArrayAccess

[](#arrayaccess)

To access to the children elements using the array API.

```
$div = div('First child', strong('Second child'), 'Third child');

echo $div[2]; //Third child
```

### IteratorAggregate

[](#iteratoraggregate)

To iterate with the element:

```
$div = div('First child', strong('Second child'), 'Third child');

foreach ($div as $child) {
    echo $child;
}
```

### Countable

[](#countable)

To use `count()`:

```
$div = div('First child', strong('Second child'), 'Third child');

echo count($div); //3
```

Note that `NULL` children are discarded

```
$div = div('First child', null, 'Third child');

echo count($div); //2
```

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance43

Moderate activity, may be stable

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.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 ~360 days

Recently: every ~538 days

Total

7

Last Release

452d ago

Major Versions

v0.1.5 → v1.0.02025-03-27

PHP version history (2 changes)v0.1.0PHP ^7.2

v1.0.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/e99df56b617f4af8cf0556a51a0ca20c7420104920c57d7e9eab093f92bb744f?d=identicon)[oscarotero](/maintainers/oscarotero)

![](https://www.gravatar.com/avatar/42e0d72f42eb7d84f67e20d28606da42e5a3248ca908b1eadb4366aafeae2561?d=identicon)[filisko](/maintainers/filisko)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (15 commits)")[![filisko](https://avatars.githubusercontent.com/u/8798694?v=4)](https://github.com/filisko "filisko (3 commits)")

---

Tags

htmlhtml-templatinghtml

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-html

A fluent html builder

8307.1M91](/packages/spatie-laravel-html)[ckeditor/ckeditor

JavaScript WYSIWYG web text editor.

5244.3M79](/packages/ckeditor-ckeditor)[caxy/php-htmldiff

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

21421.8M20](/packages/caxy-php-htmldiff)[yajra/laravel-datatables-html

Laravel DataTables HTML builder plugin

28410.1M49](/packages/yajra-laravel-datatables-html)[wa72/htmlpagedom

jQuery-inspired DOM manipulation extension for Symfony's Crawler

3394.0M34](/packages/wa72-htmlpagedom)[tinymce/tinymce

Web based JavaScript HTML WYSIWYG editor control.

1707.9M118](/packages/tinymce-tinymce)

PHPackages © 2026

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