Skip to content

Instantly share code, notes, and snippets.

@outsinre
Created March 18, 2024 07:00
Show Gist options
  • Save outsinre/022163819740c29da2a6949cce9f1f4b to your computer and use it in GitHub Desktop.
Save outsinre/022163819740c29da2a6949cce9f1f4b to your computer and use it in GitHub Desktop.
A example of Lua template
#!/usr/bin/env lua
local pl_template = require "pl.template"
local tmpl_str = [[
<ul>
# for i,val in ipairs(T) do
<li>$(i) = $(val:upper())</li>
# end
</ul>
]]
local opts = {
chunk_name = "TMP",
escape = [[#]],
inline_escape = [[$]],
inline_brackets = [[()]],
debug = true,
}
local ct, err
ct, err = pl_template.compile(tmpl_str, opts)
if err then
error("compile err: " .. err)
end
local env = {
T = { "one", "two", "three" },
}
local parent = { ipairs = ipairs } -- or parent = _G
local rt
rt, err = ct:render(env, parent, true)
if err then
error("render err: " .. err)
end
print("rendered template:")
print(rt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment