Home

Awesome

slt2

slt2 is a Lua template processor. Similar to php or jsp, you can embed lua code directly.

Installation

luarocks install slt2

Example

local slt2 = require('slt2')

local user = {
  name = '<world>'
}

function escapeHTML(str)
  local tt = {
    ['&'] = '&amp;',
    ['<'] = '&lt;',
    ['>'] = '&gt;',
    ['"'] = '&quot;',
    ["'"] = '&#39;',
  }
  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}))

Template Syntax

NOTE: don't specify a cyclic inclusion

API Reference

slt2.loadstring(template, start_tag, end_tag, tmpl_name)

slt2.loadfile(filename, start_tag, end_tag)

"Compile" the template from a string or a file, return compiled object.

slt2.render_co(f, env)

Return a coroutine function which yields a chunk of result every time. You can coroutine.create or coroutine.wrap on it.

slt2.render(f, env)

Return render result as a string.

Standalone commands

Compatibility

slt2 has been tested on:

Other versions of Lua are not tested.

Links

License

MIT License

Contribute

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.

Related work

Other Lua template engines: