validaide / html-builder
PHP Library providing a fluent interface to generate small snippets of HTML
Installs: 52 103
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 4
Requires
- php: ^8.1
- exercise/htmlpurifier-bundle: ^4.0
Requires (Dev)
- ext-tidy: *
- brianium/paratest: ^6.4
- jetbrains/phpstorm-attributes: ^1.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.0
- rector/rector: ^0.17.7
- dev-master
- 7.4.0
- 7.3.0
- 7.2.0
- 7.1.0
- 7.0.0
- 6.2.0
- 6.1.0
- 6.0.0
- 5.9.0
- 5.8.0
- 5.7.0
- 5.6.0
- 5.5.0
- 5.4.0
- 5.3.2
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 4.7.0
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.0
- 4.0.0
- 3.7.0
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.0
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.13.0
- 1.12.0
- 1.11.0
- 1.10.1
- 1.10.0
- 1.9.0
- 1.8.0
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.0
- 1.5.0
- 0.1.0
- dev-dependabot/composer/symfony/var-dumper-6.4.15
- dev-dependabot/composer/symfony/http-foundation-6.4.14
- dev-dependabot/composer/symfony/process-6.4.14
- dev-development
- dev-chore/184089749_reverse_tabnabbing
- dev-chore/184007856_drop_php74
- dev-chore/20220908_fix_temp_folder
- dev-chore/181096300_sanitizing_xss_output
- dev-chore/180175084
This package is auto-updated.
Last update: 2024-12-03 02:34:42 UTC
README
Validaide's HTML builder is a small library with a fluent interface to generate snippets of HTML code.
Introduction
Alright, so some might say: "Why in God's name would you need such a thing!?". And indeed, we have, I don't know, awesome stuff like Twig, right!? And you are right! I ❤️ Twig! But in turns out our code base still finds itself with small helper methods that generate tiny snippets of HTML.
Take the example below:
public function userStateToIcon(User $user): string { return sprintf('<span class="%s" id="%s" data-username="%s"></span>', strtolower($user->getState()), $user->getId(), $user->getUsername()): }
Now, despite that the above can be optimized still, the last return statement is the one we are trying to simplify:
public function userStateToIcon(User $user): HTMLTag { return HTML::create('span') ->class(strtolower($user->getState()) ->id($user->getId()) ->attr('data-username',$user->getUsername()); }
Now, in number of characters written, it is not necessarily faster, but it will ensure:
- Valid HTML is generated
- Safe HTML is generated
- Easier modification of the 'HTML' being built afterwards
Installation
Our big friend Composer to the rescue using Packagist of course:
composer require validaide/html-builder
❤️ Composer ❤️ Packagist
Examples
- Plain tag:
HTML::create('span')
<span></span>
- Plain tag with content:
HTML::create('h1')->text('Heading 1');
<h1>Heading 1</h1>
- Nested tags:
HTML::create('div')->id('div-1)->tag('div')->id('div-2);
<div id="div-1"><div id="div-2"></div></div>