Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lloydzhou/lua-resty-tmpl
Simple template engine for lua and openresty
https://github.com/lloydzhou/lua-resty-tmpl
Last synced: 9 days ago
JSON representation
Simple template engine for lua and openresty
- Host: GitHub
- URL: https://github.com/lloydzhou/lua-resty-tmpl
- Owner: lloydzhou
- License: mit
- Created: 2015-12-27T09:59:57.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-01-08T02:28:00.000Z (almost 9 years ago)
- Last Synced: 2024-04-16T15:08:04.918Z (7 months ago)
- Language: Lua
- Size: 10.7 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-resty - lua-resty-tmpl - template](https://github.com/dannote/lua-template). (Libraries)
README
# lua-resy-tmpl
Simple template engine for lua and openresty# Methods
## new
`syntax: t = template.new(callback, minify, tags, cacheable)`create new template instance, all params is optional
the default value of callback is "io.write" or "ngx.print"
the default value of minify is false
the default value of tags is {"{{", "}}", "{%%", "%%}"}
the default value of cacheable is true## parse
`syntax: code = t:parse(tmpl, minify)`parse the template into lua code.
## compile
`syntax: func = t:compile(tmpl, minify, cacheable)`compile the template into lua function, this method will call parse method.
this method will auto cache the compiled function.## render
`syntax: t:render(tmpl_or_func, data, callback, minify, cacheable)`render the template with variables stored in data,
the first param can be template string or compiled function.
the callback, minify and cacheable params will replace the default value gived from "new" method.# Template Syntax
In short, statements must be included between percent signs and expressions must be placed between brackets.
## Variables and expressions
variables or expressions will be replaced
{{title}}
{{ title .. "-" .. subtitle }}## Conditional and Loops
{% if messages then %}
{% for i, message in ipairs(messages) do %}
{{message}}
{% end %}
{% end %}## Set Variables
{% local testval = 'set var in template.' %}{{ testval }}# Demo
the test demo in file "test.lua"
local template = require "lib.resty.template"
local t = template.new()
local tmpl = [[
{{title}}
{{title .. " -" .. '- ' .. subtitle}}
{% if messages then %}
{% for i, message in ipairs(messages) do %}
{{message}}
{% end %}
{% end %}
{% local testvar = 'set val in template'%}{{ testvar }}
]]
local data = {title = 'Title', subtitle = 'Sub Title', messages = {'test message 1', 'test message 2'}}
t:render(tmpl, data)will get result:
Title
Title -- Sub Title
test message 1
test message 2
set val in template