English | Español
PHP library for building web components such as lists, buttons, dropdowns, and more.
- PHP 8.0 or higher
- Composer
- A web server (Apache) or PHP's built-in server
The library generates plain HTML and does not depend on any CSS framework.
Bootstrap is just an example used in examples.php; any framework (or none)
works via addClass().
# Clone this repository
$ git clone https://github.com/hstanleycrow/EasyPHPWebComponents.git
cd EasyPHPWebComponents
composer installOr using Composer
$ composer require hstanleycrow/easyphpwebcomponentsComponents render plain HTML; you add CSS classes yourself with addClass().
The examples.php example uses Bootstrap 5.3.8 and Font Awesome 6.4.2, but
they are optional, not a requirement of the library.
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" crossorigin="anonymous">use hstanleycrow\EasyPHPWebComponents\Icon;
use hstanleycrow\EasyPHPWebComponents\Button;
require 'vendor/autoload.php';
$buttonText = (new Icon('fa fa-home', 'Submit'))->render();
echo (new Button($buttonText))
->addClass('btn btn-primary')
->setId('my-button')
->render();A full example is available in `examples.php`.Icon– Renders an icon with text using CSS classes (e.g. Font Awesome).Button– A button with HTML content, classes, and attributes.Link– An anchor with HTML content and attributes (e.g.target,class).Select– A<select>dropdown with options and a selected value.UnorderedList– A<ul>list with a bullet type.OrderedList– An<ol>list with a numbering type.
All of them implement Renderable (render(): string). To build your own
component without modifying the library, just implement that interface:
use hstanleycrow\EasyPHPWebComponents\Renderable;
class Badge implements Renderable
{
public function __construct(private string $text) {}
public function render(): string
{
return '<span class="badge">' . $this->text . '</span>';
}
}| Method | Description |
|---|---|
__construct(string $iconClass, string $buttonText, string $iconPosition = 'left') |
Creates the icon. Throws InvalidArgumentException if $iconClass/$buttonText are empty or $iconPosition is not left/right. |
render(): string |
Returns the icon's HTML. |
| Method | Description |
|---|---|
__construct(string $buttonText, ?string $type = 'button') |
Throws InvalidArgumentException if $buttonText is empty. |
addClass(string $class): self |
Adds CSS classes. |
setId(string $id): self |
Sets the id. |
setDisabled(bool $disabled): self |
Toggles the disabled attribute. |
render(): string |
Returns the button's HTML. |
| Method | Description |
|---|---|
__construct(string $href, string $linkContent) |
Throws InvalidArgumentException if $href/$linkContent are empty. |
addClass(string $class): self |
Adds CSS classes. |
setRel(string $rel): self |
Sets rel. |
setTarget(string $target): self |
Sets target (default _self). |
setDownload(string $download): self |
Sets download. |
render(): string |
Returns the link's HTML. |
| Method | Description |
|---|---|
__construct(array $options = [], ?string $selected = null) |
$options is [value => label]. |
addClass(string $class): self |
Adds CSS classes. |
setId(string $id): self / getId(): string |
Select's id. |
setName(string $name): self / getName(): string |
The name attribute. |
setDisabled(bool $disabled): self |
Toggles disabled. |
setMultiple(bool $multiple): self |
Toggles multiple selection. |
render(): string |
Returns the <select> HTML with its <option>s. |
| Method | Description |
|---|---|
__construct(?string $type = '1') |
Numbering type (1, a, A, i, I). |
addItem(string $item): self |
Adds a <li>. |
setType(string $type): self |
Changes the numbering type. |
render(): string |
Returns the list's HTML. |
| Method | Description |
|---|---|
__construct(?string $type = 'circle') |
list-style-type value (circle, disc, square, none...). |
addItem(string $item): self |
Adds a <li>. |
setType(string $type): self |
Changes the list-style-type. |
addClass(string $class): self |
Adds CSS classes. |
setId(string $id): self |
Sets the id. |
render(): string |
Returns the list's HTML. |
composer testSee tests/ for the cases covered (happy path and errors) per component.
See AI_USAGE.md.