marwanalsoltany / php-html
A fluent interface for creating HTML using PHP.
Fund package maintenance!
Ko-Fi
Requires
- php: >=7.4
Requires (Dev)
- code-lts/doctum: ^5.5
- friendsofphp/php-cs-fixer: ^3.9
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-11-17 07:42:50 UTC
README
PHP-HTML
A fluent interface for creating HTML using PHP.
If you like this project and would like to support its development, giving it a ⭐ would be appreciated!
Installation
Using Composer:
Install PHP-HTML through Composer using:
composer require marwanalsoltany/php-html
Why Does This Exist?
Why not?
This is something that I exclusively created for myself. The real reason why this exist is, I really hate writing HTML within PHP code (especially in classes). If it is more than one line and it is not a template, it looks really ugly and it doesn't feel like it belongs to the code although it does. This package provides a fluent interface for the sole reason of not writing raw HTML in PHP. It also indents and validates the generated HTML and provides IDE-IntelliSense for all HTML elements to make it as easy as it gets.
Examples
Here is an example of how it works:
// on the fly echo MAKS\PhpHtml\HTML::div('This is a div!', ['class' => 'container']); // the whole deal echo (new \MAKS\PhpHtml\HTML()) ->element('h1', 'HTML Forms', ['class' => 'title']) ->open('form', ['method' => 'POST']) ->comment('SIMPLE FORM') ->h2('Example', ['class' => 'subtitle']) ->p('This is an example form.') ->br() ->if(isset($variable))->div('$variable is set') ->open('fieldset') ->legend('Form 1', ['style' => 'color: #333;']) ->label('Message: ', ['class' => 'text']) ->input(['type' => 'text', 'required']) ->entity('nbsp') ->input(['type' => 'submit', 'value' => 'Submit']) ->close() ->open('ul', ['class' => 'errors']) ->do(function () { $errors = ['Error 1', 'Error 2', 'Error 3']; foreach ($errors as $error) { $this->li($error); } }) ->close() ->close() ->render();
The generated HTML would look like this:
<!-- on the fly --> <div class="container">This is a div!</div> <!-- the whole deal --> <h1 class="title">HTML Forms</h1> <form method="POST"> <!-- SIMPLE FORM --> <h2 class="subtitle">Example</h2> <p>This is an example form.</p> <br /> <fieldset> <legend style="color: #333;">Form 1</legend> <label class="text">Message: </label> <input type="text" required /> <input type="submit" value="Submit" /> </fieldset> <ul class="errors"> <li>Error 1</li> <li>Error 2</li> <li>Error 3</li> </ul> </form>
License
PHP-HTML is an open-source project licensed under the MIT license.
Copyright (c) 2022 Marwan Al-Soltany. All rights reserved.