Перейти к содержанию

tags: Небольшой DSL для создания HTML-документов

Установка

Если вы еще не настроили подписку на репозиторий 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

Чтобы использовать эту библиотеку Lua с NGINX, убедитесь, что установлен nginx-module-lua.

Этот документ описывает lua-resty-tags v1.0, выпущенную 6 июля 2016 года.


Небольшой DSL для создания HTML-документов

Синопсис

Здесь мы определяем некоторые локальные функции:
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

Вы можете найти дополнительные советы по конфигурации и документацию для этого модуля в репозитории GitHub для nginx-module-tags.