tags: Eine kleine DSL zum Erstellen von HTML-Dokumenten
Installation
Wenn Sie noch kein RPM-Repository-Abonnement eingerichtet haben, melden Sie sich an. Dann können Sie mit den folgenden Schritten fortfahren.
CentOS/RHEL 7 oder 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
Um diese Lua-Bibliothek mit NGINX zu verwenden, stellen Sie sicher, dass nginx-module-lua installiert ist.
Dieses Dokument beschreibt lua-resty-tags v1.0, das am 06. Juli 2016 veröffentlicht wurde.
Eine kleine DSL zum Erstellen von HTML-Dokumenten
Synopsis
Hier definieren wir einige lokale Funktionen:
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"
)
)
)
)
)
Das oben genannte gibt HTML aus, das ähnlich ist wie:
<html lang="en">
<head>
<script src="main.js"></script>
</head>
<body>
<h1 class="title "is" bigger than you think" selected>
Hello
</h1>
<h1>
Another Headline
</h1>
<p>
<Beautiful> & <Strange>
<br>
table: 0x0004c370Weather
</p>
<p>
A Dog
</p>
<img src="logo.png">
<table>
<tr>
<th class="selected">
'Headline'
</th>
<th>
Headline 2
</th>
<th>
Headline 3
</th>
</tr>
</table>
</body>
</html>
Hier übergeben wir eine Funktion:
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())
Und die Ausgabe ist ähnlich:
<html lang="en">
<head>
<script src="main.js"></script>
</head>
<body>
<h1 class="title "is" bigger than you think" selected>
Hello
</h1>
<h1>
Another Headline
</h1>
<p>
<Beautiful> & <Strange>
<br>
table: 0x00054ce0Weather
</p>
<p>
A Dog
</p>
<img src="logo.png">
<table>
<tr>
<th class="selected">
'Headline'
</th>
<th>
Headline 2
</th>
<th>
Headline 3
</th>
</tr>
</table>
</body>
</html>
In diesem Beispiel erstellen wir einen Tabellenausschnitt:
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 }
})
Und hier ist die Ausgabe davon:
<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>
Einige spezielle Behandlungen werden für <script>- und <style>-Tags durchgeführt:
local tags = require "resty.tags"
local script = tags("script")
print(script[[
function hello() {
alert("<strong>Hello World</strong>");
}
hello();
]])
Wie Sie sehen können, kodieren wir die Ausgabe nicht in HTML:
<script>
function hello() {
alert("<strong>Hello World</strong>");
}
hello();
</script>
GitHub
Zusätzliche Konfigurationstipps und Dokumentationen für dieses Modul finden Sie im GitHub-Repository für nginx-module-tags.