kwebble/hadroton

此包已被 废弃 且不再维护。作者建议使用 kwebble/fillum-html 包。

一个PHP库,用于使用PHP语法创建HTML文档

0.1 2018-08-07 07:54 UTC

This package is not auto-updated.

Last update: 2019-02-20 18:59:44 UTC


README

开源PHP库,用于在代码中创建HTML 5文档。

版权 © 2016, 2017, 2018 Rob Schlüter

许可协议为MIT。详情请参阅包含的LICENSE文件。

此项目的源代码托管在GitHub上:https://github.com/kwebble/fillum-html

您可以通过我网站上的联系表单与我联系:http://kwebble.com/about

状态

此项目遵循语义化版本控制2.0.0的规则。

这意味着当前版本是不稳定的开发版本。请勿在生产环境中使用此软件。

描述

此库可以在PHP代码中生成HTML。与专门的模板系统相比,使用此库的优点

  • 无需学习和使用单独的模板语言来创建HTML输出。
  • 可以使用所有常规PHP工具来开发、重构和测试源代码。
  • 网页及其元素是PHP对象。这允许使用面向对象技术进行正常操作。
  • 没有单独的模板需要编译或解释。

使用HtmlToolbox类是生成HTML文档和元素的快捷方式。它提供了一套静态方法来创建常见的HTML元素。方法名称与它们创建的HTML元素匹配。例如,有一个创建链接的a方法,以及创建列表的olulli方法等。

    use kwebble\fillum\html\HtmlToolbox as H;

    $h = new Document();

    $h->appendBody(
        H::div(['class' => 'top'], H::h1('Welcome to Fillum HTML'))
      , H::div( ['class' => 'content']
              , H::p('This is some text.')
              , H::p('Here is another paragraph.')
        )
    );

    echo $h->toHtml();

首先创建别名H来引用HtmlToolbox类。这是工具箱的常见别名,并在整个文档中使用。

然后创建一个文档

    $h = new Document();

初始文档包含2个空元素,一个head和一个body。

最后,向文档的body中添加2个div元素。结果是具有此结构的HTML文档

    <html>
        <head></head>
        <body>
            <div class="top">
                <h1>Welcome to Fillum HTML</h1>
            </div>
            <div class="content">
                <p>This is some text.</p>
                <p>Here is another paragraph.</p>
            </div>
        </body>
    </html>

最后,使用toHtml生成HTML表示,并输出。

使用Element

工具箱不包含每个HTML元素的对应方法。如果需要工具箱未包含的元素,请使用Element类。

这是创建单个元素的方法

    $sidebar = new Element('aside');

要包含属性,请以名称和值的数组形式提供它们

    $sidebar = new Element('aside', ['id' => 'sidebar', 'class' => 'related-products']);

元素的 内容可以传递给构造函数,或者稍后添加。这里使用构造函数添加一个段落

    $sidebar = new Element('aside', [], new Element('p', [], 'You might like these'));

构造函数也接受多个元素

    $sidebar = new Element('aside', [],
        new Element('h1', [], 'Related products')
      , new Element('p', [], 'You might like these')
    );

内容可以是文本

    $p = new Element('p', [], 'Hello and welcome');

注意:Fillum HTML不会检查传递的内容结构的有效性。例如,可以将文本放在ol-元素或标题中的p-元素中。

添加数据

要向现有元素添加数据,请使用append方法。这将内容添加到所有当前元素之后

    $div = H::div();
    $div->append(H::h1('Welcome here'));

要添加多个元素,有两个选项。首先,append接受更多元素,就像构造函数一样

    $div->append(
        H::p('This is some text.')
      , H::p('Here is another paragraph.')
    );

第二种选择是使用append的流畅接口。

    $div->append(H::p('This is some text.')
        ->append(H::p('Here is another paragraph.');

允许传递数组给append

    $div = H::div();

    $text = [
        H::p('The first paragraph.')
      , H::p('Paragraph number 2.')
    ]

    $div->append($text);

创建自己的HTML结构

在大多数情况下,一个网页由多个独立的部分组成。例如,带有菜单和搜索栏的页眉,侧边栏或视频容器。

使用Fillum HTML,您可以定义这些元素为类,并让它们生成相应的HTML。这些自定义部分应该扩展Element类。

以下是一个示例类,用于简单的搜索表单,包括输入字段和提交按钮


    use kwebble\fillum\html\HtmlToolbox as H;
    use kwebble\fillum\html\Element;
    use kwebble\fillum\html\HtmlConstants;

    /** A search form.
     */
    class SearchForm extends Element {

    /** Creates the form.
     */
    public function __construct() {
        parent::__construct(
            'form'
          , ['method' => 'GET', 'action' => '/find']
          , H::input(HtmlConstants::INPUT_TYPE_TEXT, 'q')
          , H::button(HtmlConstants::BUTTON_TYPE_SUBMIT, 'Go')
        );
    }
}

如你所见,代码使用了HtmlConstants类的值。这是一个定义HTML中常见值常量的类。

SearchForm类可以用作网页的任何部分

    use kwebble\fillum\html\HtmlToolbox as H;

    $h = new Document();
    $h->appendBody(new SearchForm());

    echo $h->toHtml();

预定义常量

从文本创建元素

StringInterpreter类可以将包含HTML的字符串转换为Element

    // Assume $html is filled with HTML created elsewhere.

    use kwebble\fillum\html\Document;
    use kwebble\fillum\html\StringInterpreter;

    $h = new Document();
    $h->append((new StringInterpreter())->fromHtml($html));

    echo $h->toHtml();

可以使用定义了哪些元素和属性被保留的过滤器来清理输入。此过滤器仅允许paimg标签,a元素中的hrefimg元素中的src

    use kwebble\fillum\html\Document;
    use kwebble\fillum\html\StringInterpreter;

    $allowed = [
        'p'   => [],
        'a'   => ['href'],
        'img' => ['src']
    ];

    $h = new Document();
    $h->append((new StringInterpreter($allowed))->fromHtml($html));

    echo $h->toHtml();

生成HTML输出

要生成表示文档的HTML,请使用toHtml()方法。

    use kwebble\fillum\html\Document;

    $h = new Document();

    $h->append(new Element('p', [], 'This is some text.')
      ->append(new Element('p', [], 'Here is another paragraph.');

    $h->toHtml();