vdhicts / html-element
Easily create HTML elements from PHP
Installs: 134
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/vdhicts/html-element
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpstan/phpstan: ^1.2
- phpunit/phpunit: ^9.0
README
This package allows you to easily create HTML elements from PHP. It's inspired by David Walsh's blogpost but further improved.
use Vdhicts\HtmlElement\HtmlElement; // Create new html element with attribute name $selectElement = new HtmlElement('select'); $selectElement->setAttribute('name', 'something'); $selectElement->generate(); // Create new html element with text and attribute $paragraphElement = new HtmlElement('p', 'text', ['class' => 'center']);
Requirements
This package requires PHP 7.4 or higher.
Installation
You can install the package via composer:
composer require vdhicts/html-element
Usage
A new html element can be created with:
use Vdhicts\HtmlElement\HtmlElement; $containerElement = new HtmlElement('div'); $containerElement->setAttribute('class', 'container');
The element could contain text:
$containerElement->setText('Hello World');
The element could have one or more attributes:
// One attribute $containerElement->setAttribute('class', 'container'); // Multiple attributes $containerElement->setAttributes(['class' => 'container', 'role' => 'container']);
Or the element could contain another html element:
$colElement = new HtmlElement('div', '', ['class' => 'col-md-6']); $rowElement = new HtmlElement('div', '', ['class' => 'row']); $rowElement->inject($colElement); $containerElement->inject($rowElement);
The text and attributes can also be added, which merges with existing text or attributes.
Methods
getTagorsetTagto request or change the tag.getAttributesorgetAttribute(attribute)to retrieve all attributes with their values or one attributes with its valuesetAttributesorsetAttribute(attribute, value)to manipulate all attributes or just one.addAttributeValueadds a value to an attributes.removeAttributesorremoveAttribute(attribute)to remove all attributes or just one.removeAttributeValue(attribute, value)to remove one value from a attribute.getText,addTextorsetTextto retrieve, add or set the inner text.injectto inject a Element into the current Element.generategenerates the Element and returns its string representation.outputechos the result ofgenerate.
Tests
Unit tests are available in the tests folder. Run with:
composer test
When you want a code coverage report which will be generated in the build/report folder. Run with:
composer test-coverage
Contribution
Any contribution is welcome, but it should meet the PSR-12 standard and please create one pull request per feature/bug. In exchange, you will be credited as contributor on this page.
Security
If you discover any security related issues in this or other packages of Vdhicts, please email security@vdhicts.nl instead of using the issue tracker.
Support
If you encounter a problem with this parser or has a question about it, feel free to open an issue on GitHub.
License
This package is open-sourced software licensed under the MIT license.
About Vdhicts
Vdhicts is the name of my personal company for which I work as freelancer. Vdhicts develops and implements IT solutions for businesses and educational institutions.