OpenResty的lua-resty-template使用
来源:互联网 发布:加工中心仿真软件 编辑:程序博客网 时间:2024/06/18 12:06
OpenResty和TOMCAT服务器类似的提供了模板渲染的功能,类似于JAVA Web开发中的JSP。lua-resty-template插件模块并不是 OpenResty官方的,官方目前提供了一个lemplate的插件,截止目前(2017-08-01)还不能作为生产环境使用。
lua-resty-template下载
下载地址:https://github.com/bungle/lua-resty-template。解压后可以看到lib/resty下面有一个template.lua,这个就是我们所需要的。可以看到在template目录中还有两个lua文件,这里我们后面再介绍。
标签语法
使用lua-resty-template需要注意下面两个变量
API使用
对于nginx.conf里面的可以使用简单配置如下:
文档参考:https://github.com/bungle/lua-resty-template http://jinnianshilongnian.iteye.com/blog/2187775
链接:http://moguhu.com/article/detail?articleId=21
lua-resty-template下载
下载地址:https://github.com/bungle/lua-resty-template。解压后可以看到lib/resty下面有一个template.lua,这个就是我们所需要的。可以看到在template目录中还有两个lua文件,这里我们后面再介绍。
标签语法
- {* expression *}:表达式(变量)转义输出,类似于Spring MVC中输出ModelAndView中的变量;
- {{ expression }}:表达式(变量)做HTML转义输出;;
- {% lua code %}:Lua代码执行块;
- {( template )}:所包含的模板文件,类似于JSP中的include标签,一般我们可以将网页通用的head和footer包含进来;
- {[ expression ]}:包含表达式文件 (表达式结果),可以这样使用:{["file.html", { message = "Hello, World" } ]},上面是官方文档给的注释,个人觉得和{(template)}没啥区别,直接在上面写表达式会报错;
- {# comment #}:代码注释(不输出、不执行);
- {-block-}...{-block-}:该标签对的内容会被识别为一个Lua代码块,(请注意block不要用成verbatim或raw了,否则会被识别为纯文本输出);
- {-verbatim-}...{-verbatim-} 或者 {-raw-}...{-raw-}:两类标签的内容不会被lua-resty-template解析,并作为纯文本输出;
使用lua-resty-template需要注意下面两个变量
template_root (set $template_root /var/www/site/templates)template_location (set $template_location /templates)如果在Nginx配置中没有这些设置,则使用ngx.var.document_root的值。 如果设置了template_location,并且正常返回(状态码200),则使用其渲染。如果找不到,将回溯到template_root或document_root。
API使用
方式一:
local template = require "resty.template"-- Using template.newlocal view = template.new "view.html"view.message = "Hello, World!"view:render()方式二:
local template = require "resty.template"-- Using template.rendertemplate.render("view.html", { message = "Hello, World!" })方式三:
local template = require "resty.template"local func = template.compile("view.html") --执行函数,得到渲染之后的内容 local content = func(context) ngx.say(content)nginx配置
对于nginx.conf里面的可以使用简单配置如下:
http { server { location /test { default_type text/html; content_by_lua_file 'article/index.lua'; } }}完整实例请参见:https://github.com/Fouy/moguhu-release
文档参考:https://github.com/bungle/lua-resty-template http://jinnianshilongnian.iteye.com/blog/2187775
链接:http://moguhu.com/article/detail?articleId=21
阅读全文
1 0
- OpenResty的lua-resty-template使用
- lua-resty-kafka的使用
- 一个基于OpenResty的仿Yii的web框架 https://github.com/hylun/lua-resty-yii
- lua-resty-yii一个基于OpenResty的仿Yii的web框架
- OpenResty+lua的方案
- lua-resty-ssdb
- lua-resty-ssdb
- 通过lua-resty-upload实现文件上传的例子
- 基于 lua-resty-upload 实现简单的文件上传服务
- Lua:Nginx Lua环境配置,使用openresty
- 配置openresty使用lua并发请求API
- openresty的初步使用
- openresty-lua代码的语法检查
- lua-string-resty的aes加密解密,无法和python的aes进行对接
- pycrypto 和 lua-resty-rsa 进行跨语言的RSA加密解密.md
- windows 下 OpenResty 的使用
- 使用Nginx+Lua(OpenResty)开发高性能Web应用
- 使用Nginx+Lua(OpenResty)开发高性能Web应用
- 实例探索Java模式之路——命令模式
- PYTHON机器学习实战——最近邻KNN分类器
- Mardown常用指令
- 几个简单的bash shell命令
- FTP
- OpenResty的lua-resty-template使用
- 页面链接地址写法
- 全球首创变色龙隐形二维码,将你的图片变成可“点击”状态
- IDEA-编辑自定义代码补全
- Smart Form不弹出假脱机设置界面直接打印预览
- 阿里开源UItraViewPage源码分析
- 老树微博,三千诗与画
- 无权最短路径选择
- JavaScript速学记录