Created
March 18, 2024 07:00
-
-
Save outsinre/022163819740c29da2a6949cce9f1f4b to your computer and use it in GitHub Desktop.
A example of Lua template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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