Skip to content

newestbie/slt2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

slt2

slt2 is a Lua template processor.

Example

see test.lua

local slt2 = require('slt2')

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

function escapeHTML(str)
	local tt = {
		['<'] = '&lt;',
		['>'] = '&gt;'
	}
	str = string.gsub(str, '&', '&amp;')
	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}))

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 " }" NOTE: there is a space before "}". This is to allow table definition like "t = {}" in the embedded lua code.

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.

Compatibility

slt2 has tested on:

  • Lua 5.1
  • Lua 5.2
  • luajit 2.0

Other versions of Lua are not tested.

Links

About

a simple Lua template processor

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 100.0%