Tiny PHP HTML Builder Library

v1.0.1 2018-10-21 07:46 UTC

This package is auto-updated.

Last update: 2024-05-21 20:47:47 UTC


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.

(Fork of https://github.com/timostamm/html-builder - PHP 7 support and releases)

###Continuous integration

Build Status Code coverage GitHub version Packagist version


function generateBarcodeHtml( $barcode ) {
	$generator = new BarcodeGeneratorHTML();
	$htmlCode = $generator->getBarcode( $$barcode->toString(), $$barcode->getType(), 1, 45 );
	$el = Html::element('div');
		->html( $htmlCode )
		->text( $normalizedValue->toString() )
	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);
			->attr('href', '/' . $item['url_key'] . '.html')
			->text( $item['title'] )
			->appendTo( $li );
	return (string)$ul;

Factory methods


Creates an element.


Creates a comment node.


Creates a text node.

Appending elements


Add the new element as the last child.

$el->appendTo(Element $target)

Add the element as the targets last child.


Add the new element as the first child.

$el->prependTo($Element $target)

Add the element as the targets first child.

Element Attributes

$el->attr($name, $value)

Sets the attribute with the given name and value.


Returns the attribute with the given name.


Returns all attributes as an associative array.


Removes the attribute with the given name.

Element html


Adds a raw html string as a child.


Returns the html representation of the content.

$el->__toString() / $el->toString()

Returns the node as html.

Element text


Adds a text node.


Returns the text contents of this node including its descendants.

Element classes


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


Returns true if the element has the class.

$el->toggleClass($class, $state)

Remove or add the class.

get classes

Use $el->attr("class")

Element tag name


Returns the tag name.


Changes the tag name.