Lightweight utility class for building small snippets of HTML in PHP.
All examples expect a use Comsolit\HTMLBuilder;
statement.
$builder = new HTMLBuilder('a', 'comsolit AG');
$builder->addAttribute('href', 'https://www.comsolit.com');
$this->assertEquals(
'<a href="https://www.comsolit.com">comsolit AG</a>',
$builder->build()
);
$builder = new HTMLBuilder('input');
$builder
->setVoid() // don't build a closing tag
->addAttribute('type', 'text')
->addAttribute('name', 'some_name');
$this->assertEquals(
'<input type="text" name="some_name"/>',
$builder->build()
);
$builder = new HTMLBuilder('span', 'my text');
$this->assertEquals(
'<span>my text</span>',
$builder->build()
);
$builder = new HTMLBuilder('span');
$this->assertEquals(
$builder->build(),
(string)$builder
);
$inputBuilder = (new HTMLBuilder('input'))->setVoid();
$labelBuilder = new HTMLBuilder('label', $inputBuilder);
$this->assertEquals(
'<label><input/></label>',
(string)$labelBuilder
);
$inputBuilder = (new HTMLBuilder('input'))->setVoid();
$labelBuilder = $inputBuilder->encapsulate('label');
$this->assertEquals(
'<label><input/></label>',
(string)$labelBuilder
);
$builder = (new HTMLBuilder('input'))
->setVoid()
->addAttribute('type', 'checkbox')
->addAttributeIf(1 + 1 === 3, 'disabled', 'disabled')
->addAttributeIf(1 + 1 === 2, 'checked', 'checked');
$this->assertEquals(
'<input type="checkbox" checked="checked"/>',
(string)$builder
);
$builder = (new HTMLBuilder('span'))
->addClass('once')
->addClass('multiple')
->addClass('multiple');
$this->assertEquals(
'<span class="once multiple"></span>',
(string)$builder
);
$builder = new HTMLBuilder('ul');
for ($i = 0; $i < 2; ++$i) {
$builder->addChild(new HTMLBuilder('li', (string)$i));
}
$this->assertEquals(
'<ul><li>0</li><li>1</li></ul>',
(string)$builder
);
We've used this builder already in three projects and than evaluated other packages to decide whether we want to publish this builder or not:
- QueryPath
- FluentDOM
- php-html-generation-class - last commit 2012, 9 commits total, not on packagist
- php-class-html-generator - just a snippet, no package
- phpquery - last commit 2011
- ratrijs/html-builder
- howlowck/html-builder
- jleagle/html-builder