下载 http://luarocks.org/modules/bungle/lua-resty-template
将template.lua文件复制到openresty/lualib/resty/目录下
2.1 修改nginx.conf
设置模板路径:set $template_root /usr/local/openresty/nginx/html/templates;
设置mimeType类型:default_type text/html;
导入lua文件:content_by_lua_file /usr/local/openresty/nginx/conf/*.lua
2.2 编写lua脚本文件
修改openresty中nginx配置文件nginx.conf
set $template_root /usr/local/openresty/nginx/html/templates;location / {# root html;# index index.html index.htm;default_type text/html;content_by_lua_file /home/luafile/demo.lua;}
编写demo.lua脚本
local template = require "resty.template"
content={ message = "Hello, World!",names = {"james","fox","tony"}
}
template.render("demo.html", content)
在模板路径中创建demo.html
openresty template Example {{message}}
{%for i,v in ipairs(names) do%}{{i}}--{{v}}
{%end%}
重启openresty,进行测试