使用简洁方便的语法生成HTML。

2.1.0 2022-09-09 11:08 UTC

This package is auto-updated.

Last update: 2024-09-09 15:20:53 UTC


README

Packagist Packagist Version

入门

您可以通过composer安装此包,如果您没有composer,可以在此下载

composer require felixdorn/tag

或者在您的composer.json中添加依赖项

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

用法

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

添加属性

我们使用魔术方法 __call 来添加属性。您可以添加任何想要的属性。

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

要添加包含短横线的属性,如 aria-hidden,请使用其驼峰式等价物(ariaHidden)。在渲染标签时,我们会将其转换回短横线形式。

您也可以一次性分配多个属性。

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

嵌套标签

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')    
    ]);

致谢

许可证

本项目遵循MIT许可证。