best-served-cold/html-builder

HTML 构建器 - 通过编程构建 HTML。

2.0.7 2017-03-12 14:57 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:40:33 UTC


README

Build Status Build Status Code Coverage Latest Stable Version Scrutinizer Code Quality SensioLabsInsight

HTML 构建器

HTML5 基本构建器。我们不建议您用它来构建整个页面,但偶尔您可能需要程序化地创建 HTML,例如创建一个表格,这就是这个工具的用途!

安装

composer require best-served-cold/html-builder

使用方法

调用构建器并输出类

use BestServedCold\HTMLBuilder\Html;
use BestServedCold\HTMLBuilder\Output;

创建一个元素

$p = Html::p()->content('some content');
echo (new Output($p))->get(); 

返回值

<p>
    some content
</p>

添加属性

$div  = Html::div()->class('someClass')->id('someId');
echo (new Output($div))->get();

返回值

<div class="someClass" id="someId">
</div>

添加子元素

$div2 = Html::div(
    Html::p()->content('child content')->class('someClass'),
    function () {
        return Html::input()
            ->type('text')
            ->name('test')
            ->disabled();
    }
)->onblur('somefunc();');
echo (new Output($div2))->get();

注意:接受数组和闭包。

返回值

<div onblur="someFunc();">
    <p class="someClass">
        child content
    </p>
    <input type="text" name="test" disabled>
</div>

更改制表符大小和深度

$div3 = Html::div(
    Html::p(
        Html::span()
            ->data('bob')
            ->content('span content')
    ),
    Html::comment(' this is some comment ')

);
Output::setTabSize(2); // persistent
Output::setDepth(2); // persistent
echo (new Output($div3))->get();
Output::setTabSize(4);
Output::setDepth(0);

返回值

    <div>
      <p>
        <span data="bob">
          span content
        </span>
      </p>
      <!-- this is some comment -->
    </div>

基本表格

$table = Html::table(
    Html::thead(
        Html::tr(
            Html::th()->content('mary')->class('woman'),
            Html::th()->content('susan')->class('woman'),
            Html::th()->content('harry')->scope('col')
        )
    ),
    Html::tbody(
        Html::tr(
            Html::td()->content('margret')->class('woman'),
            Html::td()->content('bob')->onfocus('someFunction()'),
            Html::td()->content('skyscraper')->id('oddOneOut')
        ),
        Html::tr(
            Html::td()->content('brian')->colspan(3)
        )
    )
)->attribute('someNonStandardAttribute', 'mary');
echo (new Output($table))->get();

返回值

<table someNonStandardAttribute="mary">
    <thead>
        <tr>
            <th class="woman">
                mary
            </th>
            <th class="woman">
                susan
            </th>
            <th scope="col">
                harry
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="woman">
                margret
            </td>
            <td onfocus="someFunction()">
                bob
            </td>
            <td id="oddOneOut">
                skyscraper
            </td>
        </tr>
        <tr>
            <td colspan="3">
                brian
            </td>
        </tr>
    </tbody>
</table>

示例

运行示例

$ cd example
$ php ./example.php