跳转至

tags: 一个用于构建 HTML 文档的小型 DSL

安装

如果您还没有设置 RPM 仓库订阅,请 注册。然后您可以继续以下步骤。

CentOS/RHEL 7 或 Amazon Linux 2

yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install lua-resty-tags

CentOS/RHEL 8+、Fedora Linux、Amazon Linux 2023

dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install lua5.1-resty-tags

要在 NGINX 中使用此 Lua 库,请确保已安装 nginx-module-lua

本文档描述了 lua-resty-tags v1.0,发布于 2016 年 7 月 6 日。


一个用于构建 HTML 文档的小型 DSL

概述

在这里我们定义一些本地函数:
local tags = require "resty.tags"
local html,   head,   script,   body,   h1,   p,   table,   tr,   th,   img,   br = tags(
     "html", "head", "script", "body", "h1", "p", "table", "tr", "th", "img", "br")

print(
    html { lang = "en" } (
        head (
            script { src = "main.js" }
        ),
        body (
            h1 { class = 'title "is" bigger than you think', "selected" } "Hello",
            h1 "Another Headline",
            p (
                "<Beautiful> & <Strange>",
                br,
                { Car = "Was Stolen" },
                "Weather"
            ),
            p "A Dog",
            img { src = "logo.png" },
            table(
                tr (
                    th { class = "selected" } "'Headline'",
                    th "Headline 2",
                    th "Headline 3"
                )
            )
        )
    )
)
上述代码将输出类似于以下的 HTML
<html lang="en">
    <head>
        <script src="main.js"></script>
    </head>
    <body>
        <h1 class="title &quot;is&quot; bigger than you think" selected>
            Hello
        </h1>
        <h1>
            Another Headline
        </h1>
        <p>
            &lt;Beautiful&gt; &amp; &lt;Strange&gt;
            <br>
            table: 0x0004c370Weather
        </p>
        <p>
            A Dog
        </p>
        <img src="logo.png">
        <table>
            <tr>
                <th class="selected">
                    &#39;Headline&#39;
                </th>
                <th>
                    Headline 2
                </th>
                <th>
                    Headline 3
                </th>
            </tr>
        </table>
    </body>
</html>
在这里我们传入一个函数:
local tags = require "resty.tags"
local html = tags(function()
    return html { lang = "en"} (
        head (
            script { src = "main.js" }
        ),
        body (
            h1 { class = 'title "is" bigger than you think', "selected" } "Hello",
            h1 "Another Headline",
            p (
                "<Beautiful> & <Strange>",
                br,
                { Car = "Was Stolen" },
                "Weather"
            ),
            p "A Dog",
            img { src = "logo.png" },
            table(
                tr (
                    th { class = "selected" } "'Headline'",
                    th "Headline 2",
                    th "Headline 3"
                )
            )
        )
    )
end)
print(html())
输出也类似:
<html lang="en">
    <head>
        <script src="main.js"></script>
    </head>
    <body>
        <h1 class="title &quot;is&quot; bigger than you think" selected>
            Hello
        </h1>
        <h1>
            Another Headline
        </h1>
        <p>
            &lt;Beautiful&gt; &amp; &lt;Strange&gt;
            <br>
            table: 0x00054ce0Weather
        </p>
        <p>
            A Dog
        </p>
        <img src="logo.png">
        <table>
            <tr>
                <th class="selected">
                    &#39;Headline&#39;
                </th>
                <th>
                    Headline 2
                </th>
                <th>
                    Headline 3
                </th>
            </tr>
        </table>
    </body>
</html>
在这个例子中,我们创建了一个表格片段:
local tags = require "resty.tags"
local table = tags(function(rows)
    local table = table
    for _, row in ipairs(rows) do
        local tr = tr
        for _, col in ipairs(row) do
            tr(td(col))
        end
        table(tr)
    end
    return table
end)

print(table{
    { "A", 1, 1 },
    { "B", 2, 2 },
    { "C", 3, 3 }
})
其输出如下:
<table>
    <tr>
        <td>A</td>
        <td>1</td>
        <td>1</td>
    </tr>
    <tr>
        <td>B</td>
        <td>2</td>
        <td>2</td>
    </tr>
    <tr>
        <td>C</td>
        <td>3</td>
        <td>3</td>
    </tr>
</table>
<script><style> 标签进行了特殊处理:
local tags = require "resty.tags"
local script = tags("script")
print(script[[
    function hello() {
        alert("<strong>Hello World</strong>");
    }
    hello();
]])
如您所见,我们没有对输出进行 HTML 编码:
<script>
    function hello() {
        alert("<strong>Hello World</strong>");
    }
    hello();
</script>

GitHub

您可以在 nginx-module-tags 的 GitHub 仓库 中找到此模块的其他配置提示和文档。