Skip to content

henix/slt2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

About

a simple Lua template processor

Resources

Stars

Watchers

Forks

Packages

No packages published