gercoli/metatags

HTMLTags 类的包装器,旨在处理元标签。

dev-master 2015-02-22 00:09 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:24:34 UTC


README

GErcoli/HTMLTags 的扩展,使管理常见的元标签更加容易。

安装

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

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

使用类

MetaTags 类使用完全限定的命名空间,因此为了便于使用,请在您的 PHP 文件中添加 use GErcoli\MetaTags\MetaTags。其次,该类已被设计为静态使用,各种设置方法可以链式调用。

简单标签的示例

    MetaTags::setTitle("This is the page title");

    echo MetaTags::getTitle();
    // output:
    // "This is the page title"

    MetaTags::renderTitle();
    // output:
    // <title>This is the page title</title>

    MetaTags::setCharset("UTF-8")
        ->setDescription("This is the page \"description\".")
        ->renderAll();
    // output:
    //  <meta charset="UTF-8">
    //  <title>This is the page title</title>
    //  <meta name="description" content="This is the page &quot;description&quot;.">

自定义标签

不可避免地,会有一些您需要输出/渲染但无法轻松访问类方法的标签,为此,我添加了 addCustomTag(HTMLTag) 方法,您可以使用相关的类 \GErcoli\HTMLTags\HTMLTag 来创建 HTMLTag 并手动插入。

    // Create the custom tag via the HTMLTag class:
    $tag = (new HTMLTag("meta"))
        ->setAttribute("http-equiv","Content-Language")
        ->setAttribute("content","en");

    // Add the created tag to the MetaTags object,
    // and render only the very last tag that was added:
    MetaTags::addCustomTag($tag)->renderLast();

    // output:
    // <meta http-equiv="Content-Language" content="en">