1 导入lua-resty-template
下载 http://luarocks.org/modules/bungle/lua-resty-template
将template.lua文件复制到openresty/lualib/resty/目录下
2 示例
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
<html><head><meta charset="UTF-8"><title>openresty template Example</title></head><body>{{message}}<hr />{%for i,v in ipairs(names) do%}{{i}}--{{v}}<br>{%end%}</body>
</html>
重启openresty,进行测试