gercoli/htmltags

用于以面向对象的方式创建HTML标签的简单类。

v1.0.1 2015-02-19 05:27 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:25:55 UTC


README

用于以面向对象的方式创建HTML标签的简单类。

安装

将以下内容添加到您的composer.json文件中

{
    "require": {
        "gercoli/htmltags": "dev-master"
    }
}

使用类

HTMLTags类使用完全限定命名空间,为了方便使用,请在您的php文件中添加 use GErcoli\HTMLTags\HTMLTag。其次,这个类被设计成易于使用,允许setter方法的链式调用。

简单标签示例

    // Create an instance.
    $tag_div = new HTMLTag("div");
    
    // Add Classes A,B,C and remove B (only A & C should remain)
    $tag_div
        ->addClass("classA")
        ->addClass("classB")
        ->addClass("classC")
        ->removeClass("CLASSB")
        ->setAttribute("id","contrainer")
        ->appendContent("This is text inside of the div!");
    
    // Lets convert this tag to a formatted string!
    echo $tag_div
    
    // The string should read:
    // <div class="classA classC" id="contrainer">This is text inside of the div!</div>

嵌套HTML标签

    // Create a meta description tag.
    $tag_description = (new HTMLTag("meta"))
        ->setAttribute("name","description")
        ->setAttribute("content","this is a \"description\" tag.");

    // Create a title tag.
    $tag_title = (new HTMLTag("title"))
        ->appendContent("This is a page title");

    // Create a head tag and insert the two other tags INSIDE it.
    $tag_head = (new HTMLTag("head"))
        ->appendContent($tag_title)
        ->appendContent($tag_description);

    // Check the markup:
    echo $tag_head;
    
    /*
     * OUTPUT:
     *  <head>
	 *      <title>This is a page title</title>
	 *      <meta name="description" content="this is a &quot;description&quot; tag.">
     *  </head>
     *
     * Notice that using echo will convert the tag and all sub-tags (or children) to strings,
     * automatically escape invalid characters (such as quotes), and indent the tags.
     */

关于结束标签的说明

默认情况下,当您通过 ->appendContent()->prependContent() 向标签添加内容时,将自动启用结束标签。然而,使用 ->setClosingTag(false) 将禁用结束标签,根据 HTML5规范,这是完全有效的(这就是为什么我没有添加检查),因此请注意这个事实。

具有XHTML子元素的HTML标签

当您输出包含子标签的父标签时,父标签的格式目前不会级联到其子标签,这是设计上的。有些标签,特别是社交网络,并不总是正确验证非XHTML标签,因此,当您将标签设置为XHTML时,它将保持这种方式,直到您自己更改它。这将在未来改变,但为了使验证器满意,我将保留自闭合标签。

待办事项

  • 更好的文档(正在努力中)
  • 实现解析style属性的功能,我们不想有重复的样式。