PHPackages                             best-served-cold/html-builder - 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. best-served-cold/html-builder

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

best-served-cold/html-builder
=============================

HTML Builder - Build HTML programatically.

2.0.7(9y ago)26511MITPHPPHP &gt;=5.6

Since Mar 4Pushed 9y ago2 watchersCompare

[ Source](https://github.com/nark3d/HTMLBuilder)[ Packagist](https://packagist.org/packages/best-served-cold/html-builder)[ Docs](http://bestservedcold.com)[ RSS](/packages/best-served-cold-html-builder/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (12)Used By (1)

[![Build Status](https://camo.githubusercontent.com/be99ea3cef30f3d480ce20b69ccd24fc11948a28ea840d2368d27adbcd638645/68747470733a2f2f7472617669732d63692e6f72672f6e61726b33642f48544d4c4275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/nark3d/HTMLBuilder)[![Build Status](https://camo.githubusercontent.com/a9fbe72b47bdff82b9b62b483ed965272e6a37cf0fdb2014308e64e35e7c72b8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e61726b33642f48544d4c4275696c6465722f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nark3d/HTMLBuilder/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/f935b5a2f16b5a45dc25a34a44d4c3a4e8978553e6725b30b8908d0af5096a20/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e61726b33642f48544d4c4275696c6465722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nark3d/HTMLBuilder/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/508e4dd6f9ee13ca1a5b3323a688076c1e30cd0365298aac9ca918c59cb1d86b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f626573742d7365727665642d636f6c642f68746d6c2d6275696c6465722e737667)](https://packagist.org/packages/best-served-cold/html-builder)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/106222fb88d485278dcf42e05bdadc29ef948f807e8926230e4e32619e847a13/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e61726b33642f48544d4c4275696c6465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nark3d/HTMLBuilder/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/c4128c703872d451e2702621bd88f40a187a89faf7b5ee769c99af78511c8448/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f62386532313733662d336665312d346664352d393963322d3534376433623333616566362f6d696e692e706e67)](https://insight.sensiolabs.com/projects/b8e2173f-3fe1-4fd5-99c2-547d3b33aef6)

HTML Builder
============

[](#html-builder)

Basic HTML builder for HTML5. We're not suggesting that you use this to build entire pages, but occasionally there is a need to create HTML programatically, like a table, and that's what this is for!

Install
-------

[](#install)

```
composer require best-served-cold/html-builder
```

Usage
-----

[](#usage)

Call in the Builder and Output classes:

```
use BestServedCold\HTMLBuilder\Html;
use BestServedCold\HTMLBuilder\Output;
```

### Create an element

[](#create-an-element)

```
$p = Html::p()->content('some content');
echo (new Output($p))->get();
```

Returns:

```

    some content

```

### Adding attributes

[](#adding-attributes)

```
$div  = Html::div()->class('someClass')->id('someId');
echo (new Output($div))->get();
```

Returns:

```

```

### Adding children

[](#adding-children)

```
$div2 = Html::div(
    Html::p()->content('child content')->class('someClass'),
    function () {
        return Html::input()
            ->type('text')
            ->name('test')
            ->disabled();
    }
)->onblur('somefunc();');
echo (new Output($div2))->get();
```

NB: Accepts arrays as well as Closures.

Returns:

```

        child content

```

### Change tab size and depth

[](#change-tab-size-and-depth)

```
$div3 = Html::div(
    Html::p(
        Html::span()
            ->data('bob')
            ->content('span content')
    ),
    Html::comment(' this is some comment ')

);
Output::setTabSize(2); // persistent
Output::setDepth(2); // persistent
echo (new Output($div3))->get();
Output::setTabSize(4);
Output::setDepth(0);
```

Returns:

```

          span content

```

### Basic table

[](#basic-table)

```
$table = Html::table(
    Html::thead(
        Html::tr(
            Html::th()->content('mary')->class('woman'),
            Html::th()->content('susan')->class('woman'),
            Html::th()->content('harry')->scope('col')
        )
    ),
    Html::tbody(
        Html::tr(
            Html::td()->content('margret')->class('woman'),
            Html::td()->content('bob')->onfocus('someFunction()'),
            Html::td()->content('skyscraper')->id('oddOneOut')
        ),
        Html::tr(
            Html::td()->content('brian')->colspan(3)
        )
    )
)->attribute('someNonStandardAttribute', 'mary');
echo (new Output($table))->get();
```

Returns:

```

                mary

                susan

                harry

                margret

                bob

                skyscraper

                brian

```

### Examples

[](#examples)

To run the examples:

```
$ cd example
$ php ./example.php
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 94.9% 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 ~1 days

Total

11

Last Release

3351d ago

Major Versions

1.0.2 → 2.0.02017-03-10

PHP version history (2 changes)1.0.0PHP &gt;=5.6.28

1.0.1PHP &gt;=5.6

### Community

Maintainers

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

---

Top Contributors

[![nark3d](https://avatars.githubusercontent.com/u/2162621?v=4)](https://github.com/nark3d "nark3d (37 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (2 commits)")

---

Tags

htmlbuilderhtml-builderBest Served Cold

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/best-served-cold-html-builder/health.svg)

```
[![Health](https://phpackages.com/badges/best-served-cold-html-builder/health.svg)](https://phpackages.com/packages/best-served-cold-html-builder)
```

###  Alternatives

[caxy/php-htmldiff

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

21520.9M15](/packages/caxy-php-htmldiff)[avplab/php-html-builder

PHP Html builder simplifies creation of an html code in php scripts. Allows to build (or generate) the html in simple natural way, similarly as create a html page.

189.2k](/packages/avplab-php-html-builder)

PHPackages © 2026

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