slt2 is a Lua template processor.
see test.lua
local slt2 = require('slt2')
local user = {
name = '<world>'
}
function escapeHTML(str)
local tt = {
['<'] = '<',
['>'] = '>'
}
str = string.gsub(str, '&', '&')
str = string.gsub(str, '[<>]', tt)
return str
end
local tmpl = slt2.loadstring([[<span>
#{ if user ~= nil then }
Hello, #{= escapeHTML(user.name) }!
#{ else }
<a href="/login">login</a>
#{ end }
</span>
]])
io.write(slt2.render(tmpl, {user = user}))- #{ lua code } : embed lua code
- #{= expression } : embed lua expression
- #{include: 'file' } : include another template
NOTE: don't specify a cyclic inclusion
"Compile" the template from a string or a file, return compiled object.
- start_tag: default "#{"
- end_tag: default " }" NOTE: there is a space before "}". This is to allow table definition like "t = {}" in the embedded lua code.
Return a coroutine function which yields a chunk of result every time. You can coroutine.create or coroutine.wrap on it.
Return render result as a string.
slt2 has tested on:
- Lua 5.1
- Lua 5.2
- luajit 2.0
Other versions of Lua are not tested.
- slt
- Simple Lua Template 0.1 发布 (Chinese)