Lua Template is an efficient template engine with support for conditional rendering, loops, includes, and substitutions with escaping.
Here is a quick example.
Template:
<p>Hello, ${name}.</p>
<l:if cond="hour < 12"><p>It is morning.</p><l:else/><p>It is afternoon.</l:if>
<p>Here are your TODOs:</p>
<ul>
<l:for names="_, todo" in="ipairs(todos)">
<li><a href="/todos/$[u]{todo.id}">${todo.text}</a></li></l:for>
</ul>Code:
local template = require("template")
-- Prepare environment
local env = setmetatable({
name = "Peter",
hour = 8,
todos = {
{ id = 1, name = "TODO #1" },
{ id = 2, name = "TODO #2" },
{ id = 3, name = "TODO #3" },
}
}, { __index = _G })
-- Render to the standard output
template.render("template.html", env, io.stdout)Output:
<p>Hello, Peter.</p>
<p>It is morning.</p>
<p>Here are your TODOs:</p>
<ul>
<li><a href="/todos/1">TODO #1</a></li>
<li><a href="/todos/2">TODO #2</a></li>
<li><a href="/todos/3">TODO #3</a></li>
</ul>To build and install with LuaRocks, run:
luarocks install lua-template
Lua Template comes with a simple Makefile. Please adapt the Makefile to your environment, and then run:
make
make test
make install
Please see the release notes document.
Please see the documentation folder.
Lua Template supports Lua 5.3 and Lua 5.4.
Lua Template does not support the closing value in the generic for loop.
Lua Template has been built and tested on Ubuntu Linux (64-bit).
Lua Template is released under the MIT license. See LICENSE for license terms.