Generates HTML in a clean and convenient syntax.

2.1.0 2022-09-09 11:08 UTC

This package is auto-updated.

Last update: 2024-12-09 15:45:50 UTC


README

Packagist Packagist Version

Getting started

You can install the package via composer, if you don't have composer, you can download it here:

composer require felixdorn/tag

Or by adding a requirement in your composer.json:

{
    "require": {
        "felixdorn/tag": "^2.0.0"
    }
}

Usage

$tag = tag('div')
    ->class('container')
    ->children(
        tag('h1')->children('Title'),
        'Some raw content'
    );

Adding attributes

We use the magic method __call to add attributes. You can add any attribute you want.

tag('div')
    ->class('alert alert-danger')
    ->children('Something went wrong');

To add attributes containing a dash like aria-hidden, use the equivalent in camel case (ariaHidden). We'll do the conversion back to kebab case when rendering the tag.

You can also assign multiple attributes at once.

tag('div')
    ->attributes([
    'id' => 'my-div',
    'class' => 'mt-2'
]);

Nested tags

tag('div')
    ->class('container')
    ->children(
        tag('h1')->children('Some text')
    );

tag('div')
    ->children([
        tag('h1')->children(
            tag('span')->children('Hello'),
            tag('b')->children('World'),
        ),
        tag('br'),
        tag('h3')->children('Whatever')    
    ]);

Credits

License

This project is MIT Licensed.