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

ActiveLibrary

timostamm/html-builder
======================

Tiny PHP HTML Builder Library

27.8k4PHP

Since Sep 12Pushed 7y ago2 watchersCompare

[ Source](https://github.com/timostamm/html-builder)[ Packagist](https://packagist.org/packages/timostamm/html-builder)[ RSS](/packages/timostamm-html-builder/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

PHP HTML Builder
================

[](#php-html-builder)

[![Build Status](https://camo.githubusercontent.com/8142cc7f77539f03628d51860987210ed65d9d29e5b4374075229a6302cd8e9f/68747470733a2f2f6170692e7472617669732d63692e6f72672f74696d6f7374616d6d2f68746d6c2d6275696c6465722e706e67)](https://travis-ci.org/timostamm/html-builder)

A simple library to generate HTML fragments with a jquery-like interface.

This library does not parse HTML, and it does not provide element lookup via selectors or otherwise. It is focused on building simple HTML fragments, for example embed codes, while taking care of proper escaping and other simple syntax rules. Its purpose is to replace manual concatenation of strings.

### Examples

[](#examples)

```
function generateBarcodeHtml( $barcode ) {

	$generator = new BarcodeGeneratorHTML();
	$htmlCode = $generator->getBarcode( $$barcode->toString(), $$barcode->getType(), 1, 45 );

	$el = Html::element('div');
	$el->addClass('barcode-container');

	Html::element('div')
		->addClass('barcode-bars')
		->html( $htmlCode )
		->appendTo($el);

	Html::element('div')
		->addClass('barcode-number')
		->text( $normalizedValue->toString() )
		->appendTo($el);

	return $el;
}

public function generateMenu(array $items) {

	$ul = Html::element('ul');

	foreach ( $this->items as $item ) {

		$li = Html::element('li')
			->appendTo( $ul )
			->toggleClass('active', $item['id'] === $this->activeId);

		Html::element('a')
			->attr('href', '/' . $item['url_key'] . '.html')
			->text( $item['title'] )
			->appendTo( $li );

	}

	return (string)$ul;
}
```

### Factory methods

[](#factory-methods)

##### Html::element($tagName)

[](#htmlelementtagname)

Creates an element.

##### Html::comment($text)

[](#htmlcommenttext)

Creates a comment node.

##### Html::text($text)

[](#htmltexttext)

Creates a text node.

### Appending elements

[](#appending-elements)

##### $el-&gt;append($tagNameOrNode)

[](#el-appendtagnameornode)

Add the new element as the last child.

##### $el-&gt;appendTo(Element $target)

[](#el-appendtoelement-target)

Add the element as the targets last child.

##### $el-&gt;prepend($tagNameOrNode)

[](#el-prependtagnameornode)

Add the new element as the first child.

##### $el-&gt;prependTo($Element $target)

[](#el-prependtoelement-target)

Add the element as the targets first child.

### Element Attributes

[](#element-attributes)

##### $el-&gt;attr($name, $value)

[](#el-attrname-value)

Sets the attribute with the given name and value.

##### $el-&gt;attr($name)

[](#el-attrname)

Returns the attribute with the given name.

##### $el-&gt;attr()

[](#el-attr)

Returns all attributes as an associative array.

##### $el-&gt;removeAttr($name)

[](#el-removeattrname)

Removes the attribute with the given name.

### Element html

[](#element-html)

##### $el-&gt;html($str)

[](#el-htmlstr)

Adds a raw html string as a child.

##### $el-&gt;html()

[](#el-html)

Returns the html representation of the content.

##### $el-&gt;\_\_toString() / $el-&gt;toString()

[](#el-__tostring--el-tostring)

Returns the node as html.

### Element text

[](#element-text)

##### $el-&gt;html($str)

[](#el-htmlstr-1)

Adds a text node.

##### $el-&gt;text()

[](#el-text)

Returns the text contents of this node including its descendants.

### Element classes

[](#element-classes)

##### $el-&gt;addClass($class)

[](#el-addclassclass)

Add one or more classes. Multiple classes are separated by a space.

##### $el-&gt;hasClass($class)

[](#el-hasclassclass)

Returns true if the element has the class.

##### $el-&gt;toggleClass($class, $state)

[](#el-toggleclassclass-state)

Remove or add the class.

##### get classes

[](#get-classes)

Use `$el->attr("class")`

### Element tag name

[](#element-tag-name)

##### $el-&gt;getTagname()

[](#el-gettagname)

Returns the tag name.

##### $el-&gt;switchTagname($value)

[](#el-switchtagnamevalue)

Changes the tag name.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85.7% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6364011a12f22cb5046056371bde6170218218c560eddfdc08b4644b7648af78?d=identicon)[timostamm](/maintainers/timostamm)

---

Top Contributors

[![timostamm](https://avatars.githubusercontent.com/u/4289451?v=4)](https://github.com/timostamm "timostamm (6 commits)")[![bobjacobsen](https://avatars.githubusercontent.com/u/2774117?v=4)](https://github.com/bobjacobsen "bobjacobsen (1 commits)")

### Embed Badge

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

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

PHPackages © 2026

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