https://github.com/brick/html
A simple HTML 5 generation library
https://github.com/brick/html
html html-tags php
Last synced: about 1 year ago
JSON representation
A simple HTML 5 generation library
- Host: GitHub
- URL: https://github.com/brick/html
- Owner: brick
- License: mit
- Created: 2017-10-06T12:17:16.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-05-02T23:20:50.000Z (about 2 years ago)
- Last Synced: 2024-10-06T22:35:54.149Z (over 1 year ago)
- Topics: html, html-tags, php
- Language: PHP
- Homepage:
- Size: 26.4 KB
- Stars: 4
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Brick\Html

A very simple HTML 5 generation library.
[](https://github.com/brick/html/actions)
[](https://coveralls.io/github/brick/html?branch=master)
[](https://packagist.org/packages/brick/html)
[](http://opensource.org/licenses/MIT)
## Installation
This library is installable via [Composer](https://getcomposer.org/):
```bash
composer require brick/html
```
## Requirements
This library requires PHP 7.1 or later.
## Project status & release process
This library is still under development.
The current releases are numbered `0.x.y`. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), `y` is incremented.
**When a breaking change is introduced, a new `0.x` version cycle is always started.**
It is therefore safe to lock your project to a given release cycle, such as `0.1.*`.
If you need to upgrade to a newer release cycle, check the [release history](https://github.com/brick/html/releases) for a list of changes introduced by each further `0.x.0` version.
## Introduction
This library contains a single class, `Tag`, that represents an HTML tag. You construct a `Tag` using a tag name:
```php
use Brick\Html\Tag;
$div = new Tag('div');
```
### Attributes
You can pass an optional associative array of attributes to the constructor:
```php
$div = new Tag('div', [
'id' => 'main',
'class' => 'block',
]);
```
Or you can set attributes later:
```php
$tag->setAttributes([
'id' => 'main',
'class' => 'block',
]);
```
Or:
```php
$tag->setAttribute('id', 'main')
->setAttribute('class', 'block');
```
You can also remove attributes:
```php
$tag->removeAttribute('id');
```
### Content
You can set the content of a `Tag`, provided that it's not a *void* tag such as `
`, ``, etc.
If you try to modify the content of a void tag, you'll get a `LogicException`.
You can set or append a plain text content:
```php
$tag->setTextContent('Hello, world!');
$tag->appendTextContent("\nWhat's up?");
```
Or set/append a HTML content:
```php
$tag->setHtmlContent('Hello, world!');
$tag->appendHtmlContent("
What's up?");
```
You can also append another `Tag`:
```php
$tag->append($otherTag);
```
You can remove the content of a `Tag`:
```php
$tag->empty();
```
You can check if a `Tag` has an empty content:
```php
$tag->isEmpty(); // boolean
```
### Rendering a tag
You can render a tag by using its `render()` method, or just by casting it to string:
```php
echo $tag; // will output something like:
Hello, world!
```
### Encoding
All texts (attributes, content) are expected to be valid UTF-8.