Internationalisation
Bundled Plugins API
resty
lua-resty-template is a compiling (1) (HTML) templating engine for Lua and OpenResty.
(1) with compilation we mean that templates are translated to Lua functions that you may call or string.dump as a binary bytecode blobs to disk that can be later utilized with lua-resty-template or basic load and loadfile standard Lua functions (see also Template Precompilation). Although, generally you don't need to do that as lua-resty-template handles this behind the scenes.

Hello World with lua-resty-template

1
local template = require "resty.template"
2
-- Using template.new
3
local view = template.new "view.html"
4
view.message = "Hello, World!"
5
view:render()
6
-- Using template.render
7
template.render("view.html", { message = "Hello, World!" })
Copied!
view.html
1
<!DOCTYPE html>
2
<html>
3
<body>
4
<h1>{{message}}</h1>
5
</body>
6
</html>
Copied!
Output
1
<!DOCTYPE html>
2
<html>
3
<body>
4
<h1>Hello, World!</h1>
5
</body>
6
</html>
Copied!
The same can be done with inline template string:
1
-- Using template string
2
template.render([[
3
<!DOCTYPE html>
4
<html>
5
<body>
6
<h1>{{message}}</h1>
7
</body>
8
</html>]], { message = "Hello, World!" })
Copied!
Last modified 1mo ago
Export as PDF
Copy link