/html

👷 Work in progress

Primary LanguagePHPMIT LicenseMIT

authanram/html

Painless html generation.

Installation

You can install the package via composer.

composer require authanram/html

Basic Usage Example

Here's an example of how it can be used in a very basic way:

use Authanram\Html\Renderer;

$qux = [
    'tag' => 'a',
    'attributes' => [
        'href' => 'https://github.com/authanram',
        'class' => 'text-blue-600',
        'data-anchor' => true,
    ],
    'contents' => [
        'authanram at github.com'
    ],
];

Renderer::renderFromArray($qux);

Renderer::renderFromArray($qux); will return the following string:

<a href="https://github.com/authanram" class="text-blue-600" data-anchor>
    authanram at github.com
</a>

Nesting

use Authanram\Html\Renderer;

$qux = [
    'tag' => 'p',
    'contents' => [
        [
            'tag' => 'a',
            'attributes' => [
                'class' => 'text-blue-600',
                'href' => 'https://github.com/authanram',
            ],
            'contents' => [
                [
                    'tag' => 'span',
                    'class' => 'semibold',
                    'contents' => [
                        ['authanram at github.com'],
                    ]
                ],
            ],
        ],
    ],
];

Renderer::renderFromArray($qux);

Renderer::renderFromArray($qux); will return the following string:

<p>
    <a class="text-blue-600" href="https://github.com/authanram">
        <span class="semibold">
            authanram at github.com
        </span>
    </a>
</p>

Class Based Usage

As you can see here, we can achieve the same result using the static method Authanram\Html\Element::make():

use Authanram\Html\Element;

Element::make(
    'a',
    [
        'href' => 'https://gitub.com',
        'class' => 'text-blue-600',
    ],
    ['authanram at github.com'],
)->render();

Abbreviation Based Usage

...

use Authanram\Html\Element;

Element::parse('a.text-blue-600[href=https://gitub.com]')
    ->render();

Credits

License

The MIT License (MIT). Please see License File for more information.