Mastering Lua‑resty‑template: Fast HTML Rendering with OpenResty
This guide introduces the lightweight Lua‑resty‑template engine for OpenResty, explains its typical use cases, shows how to install and configure it, and provides step‑by‑step examples of basic and advanced template rendering with code snippets and configuration files.
Introduction
Lua‑testy‑template (lua‑resty‑template) is a lightweight templating engine built on OpenResty, the powerful integration of Nginx and Lua. It aims to generate dynamic HTML pages quickly inside an Nginx environment, offering a simple, efficient, and flexible way to process templates.
Typical Use Cases
Backend rendering for web services: generate dynamic HTML directly on Nginx, reducing upstream server load.
API gateway: leverage OpenResty’s capabilities to handle complex gateway logic before the request reaches the frontend.
Static site generation: although primarily for dynamic rendering, it can also be used to produce static pages in simplified scenarios.
Installation
Install via the OpenResty package manager (OPM):
#opm get bungle/lua-resty-templateCheck the installed version:
# opm info bungle/lua-resty-template
Name : lua-resty-template
Version : 2.0
Abstract : Templating Engine (HTML) for Lua and OpenResty
Author : Aapo Talvensaari (@bungle)
Code Repo : https://github.com/bungle/lua-resty-template
License : BSD 3‑Clause "New" or "Revised" licenseConfiguration
The default template root is ngx.var.document_root. You can override it with the following directives in your Nginx configuration:
template_root set $template_root /var/www/site/templates
template_location set $template_location /templatesIf template_location returns a 200 status, it is used; otherwise the engine falls back to template_root or document_root.
To inspect the value of ngx.var.document_root, you can print it:
ngx.say(ngx.var.document_root)
ngx.say(ngx.var.template_root)Typical output:
/usr/local/openresty/nginx/html -- default
/usr/local/openresty/nginx/conf/lua/view -- custom pathBasic Usage
Create hello_template.lua:
local template = require "resty.template"
local view = template.new "hello.html"
view.message = "Hello, World!"
view:render()Or use the functional API:
local template = require "resty.template"
local view = template.new "hello.html"
view.message = "Hello, World!"
template.render("hello.html", { message = "Hello, World!" })Corresponding hello.html template:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OpenResty Demo</title>
</head>
<body>
<h1>{{message}}</h1>
</body>
</html>Configure Nginx (e.g., openresty.tinywan.com.conf) to serve the template:
server {
listen 80;
server_name openresty.tinywan.com;
set $template_root /usr/local/openresty/nginx/conf/lua/view;
location /resty_template {
default_type "text/html";
lua_code_cache off;
content_by_lua_file conf/lua/hello_template.lua;
}
}Access http://openresty.tinywan.com/resty_template to see the rendered page.
Template Syntax
{{expression}}– output escaped result of the expression. {*expression*} – output raw result. {% lua code %} – execute Lua code. {(template)} – include another template file; you can pass a context map. {[expression]} – include a file whose name is the result of the expression. {-block-} … {-block-} – define a named block for reuse. {-raw-} … {-raw-} – output content verbatim without processing. {# comment #} – comment block (ignored).
Within templates you can access all keys from the context table and the template table, as well as any prefixed keys.
Advanced Usage
Render a template with additional data such as a live ID and a table of members:
local template = require "resty.template"
local live_id = ngx.var.live_id
local members = { Tom = 2020, Jake = 2024, Dodo = 2028, Jhon = 2030 }
template.render("hello.html", {
title = "OpenResty Demo",
live_id = live_id,
members = members
})Extended hello.html example:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OpenResty Demo</title>
</head>
<body>
<h1>{{title}} {{live_id}}</h1>
<ul>
{% for value, name in pairs(members) do %}
<li>{{value}} == {{name}}</li>
{% end %}
</ul>
</body>
</html>Visit http://openresty.tinywan.com/resty_template to see the rendered output.
For more information, see the project repository: https://github.com/bungle/lua-resty-template
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
