https://github.com/becklyn/html-builder
Simple helpers for building HTML
https://github.com/becklyn/html-builder
Last synced: about 1 month ago
JSON representation
Simple helpers for building HTML
- Host: GitHub
- URL: https://github.com/becklyn/html-builder
- Owner: Becklyn
- License: bsd-3-clause
- Created: 2019-05-07T14:27:03.000Z (about 6 years ago)
- Default Branch: 2.x
- Last Pushed: 2022-02-17T11:29:15.000Z (over 3 years ago)
- Last Synced: 2025-04-10T23:42:46.046Z (about 1 month ago)
- Language: PHP
- Size: 42 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
HTML Builder
============Elements Builder
----------------```php
use Becklyn\HtmlBuilder\Builder\HtmlBuilder;
use Becklyn\HtmlBuilder\Node\HtmlElement;$builder = new HtmlBuilder();
$link = new HtmlElement("a", [
"href" => "https://becklyn.com",
], [
"Becklyn Studios"
]);assert('Becklyn Studios' === $builder->buildElement($link));
```Attributes Builder
------------------```php
use Becklyn\HtmlBuilder\Builder\HtmlBuilder;
use Becklyn\HtmlBuilder\Node\HtmlAttributes;$builder = new HtmlBuilder();
$attributes = $builder->buildAttributes(new HtmlAttributes([
"href" => "https://becklyn.com",
"target" => "_blank",
]));echo "Becklyn";
```Special values:
* `false`: entry will be omitted
* `null`: will be omitted
* `true`: will be rendered as boolean attribute, eg. `"checked" => true` as ``.```php
$full = $builder->build([
"first" => "a",
"removed1" => false,
"removed2" => null,
"checked" => true,
"last" => "b",
]);assert($full === 'first="a" checked last="b"'); // true
```Adding pre-compiled HTML to an element
--------------------------------------To avoid automatic escaping of the content, you can use `SafeMarkup`:
```php
$link = new HtmlElement("div");
$link->addContent(new SafeMarkup("This will not be escaped!"));assert('
This will not be escaped!' === $builder->buildElement($link));
```