slt2 is a Lua template processor. Similar to php or jsp, you can embed lua code directly.
luarocks install slt2
local slt2 = require('slt2')
local user = {
  name = '<world>'
}
function escapeHTML(str)
  local tt = {
    ['&'] = '&',
    ['<'] = '<',
    ['>'] = '>',
    ['"'] = '"',
    ["'"] = ''',
  }
  local r = string.gsub(str, '[&<>"\']', tt)
  return r
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 "}#"
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.
- runslt2: render a template with a lua table value
slt2 has been tested on:
- Lua 5.1
- Lua 5.2
- luajit 2.0
Other versions of Lua are not tested.
- Simple Lua Template (Chinese)
MIT License
Please create an issue, explaining what's the problem you are trying to solve, before you send a pull request. See issue #5 for an example.
- Run tests: ./tests.sh
- Run tests for different lua implementation: LUA=luajit ./tests.sh
Other Lua template engines: