https://github.com/bungle/lua-resty-templatehttps://github.com/bungle/lua-resty-template解压后:将 lib里面的文件(template.lua + template目录)复制到 openresty-1.21.4.1-win64\lualib\resty下
注:模板错误信息输出在 Nginx 日志中 log\error.log
Nginx 配置
http{
init_by_lua_block {
template = require 'resty.template'
}
server {
......
set $template_root ./site/demo/tpl;
location /tpl
{
default_type text/html;
content_by_lua "
local html = template.compile('view.html')({ message = 'Hello, World!' })
ngx.print(html)
";
}
......
}
}
view.html
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>{{message}}</title>
</head>
<body>
<h1>{{message}}</h1>
</body>
</html>
<html>
<body>
<ul>
{% for _, user in ipairs(users) do %}
{(user.html, user)}
{% end %}
</ul>
</body>
</html>
user.html
<li>User {{name}} is of age {{age}}</li>
<html>
<body>
{% if state==1 then %}
abc
{% elseif state==2 then %}
123
{% else %}
...
{% end %}
</body>
</html>