a simple Lua template processor
Lua Shell
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
tests Add test: include file not found Feb 16, 2016
.gitignore Add .src.rock to .gitignore Feb 15, 2016
README.md add related work Feb 17, 2016
runslt2.lua
slt2-1.0-1.rockspec
slt2.lua Change indentation from tab to 2 spaces Feb 15, 2016
tests.sh Allow omit empty data.lua Feb 16, 2016

README.md

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

  • #{ lua code }# : embed lua code
  • #{= expression }# : embed lua expression
  • #{include: 'file' }# : include another template

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.

  • start_tag: default "#{"
  • end_tag: default "}#"

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

  • runslt2: render a template with a lua table value

Compatibility

slt2 has been tested on:

  • Lua 5.1
  • Lua 5.2
  • luajit 2.0

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.

  • Run tests: ./tests.sh
  • Run tests for different lua implementation: LUA=luajit ./tests.sh

Related work

Other Lua template engines: