OpenResty 中模板渲染引擎 lua-resty-template
OpenResty Lua LuaRocks 大约 1846 字项目介绍
lua-resty-template
是Lua
和OpenResty
中的模板渲染引擎。由Kong网关
核心工程师bungle
开源,京东商城
使用此项目完成相关开发。
开源地址:https://github.com/bungle/lua-resty-template
快速入门
安装
luarocks install lua-resty-template
content_by_lua_file
local template = require "resty.template"
template.render("post.html", { message = "Hello, Post!" })
post.html
<!DOCTYPE html>
<html>
<body>
<h1>{{message}}</h1>
</body>
</html>
输出
<!DOCTYPE html>
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
模板语法
{{expression}}
:输出传递的值,转义html
相关标签{*expression*}
:输出传递的值{% lua code %}
:使用lua
代码{(template)}
:引入html
共用页面{(base.html, { title = "Hello, World" } )}
:引入html
共用页面,并传递相关值{-verbatim-}...{-verbatim-}
/{-raw-}...{-raw-}
:可原样输出模板语法{# comments #}
:在模板中使用注释,不会被执行和输出
更多详细语法请查阅官方文档:https://github.com/bungle/lua-resty-template
简要示例
{{expression}}
message = "<h1>Hello World</h1>"
输出:<h1>Hello World</h1>
<body>
{{message}}
</body
{*expression*}
message = "<h1>Hello World</h1>"
输出:Hello World
(带h1
样式)
<body>
{*message*}
</body
{% lua code %}
使用if
判断。
{% if 1 == 2 then %}
<h1>1 = 2</h1>
{% else %}
<h1>1 ~= 2</h1>
{% end %}
{(template)}
引入共用模板,传递title
、keywords
、description
等属性。
{(base.html, { title = ngx.ctx.post.title, keywords = ngx.ctx.post.keywords, description = ngx.ctx.post.description } )}
base.html
<title>{{title}} | 程序员技术之旅</title>
<meta name="keywords" content="{{keywords}}"/>
<meta name="description" content="{{description}}"/>
{-raw-}...{-raw-}
可原样输出lua-resty-template
中的语法。
输出:{{message}}
。
<body>
{-raw-}{{message}}{-raw-}
</body>
{# comments #}
模板中注释,帮助开发者记录,不会被执行和输出。
<body>
内容123
{# This is comment #}
内容456
</body>
开源案例
阅读 5599 · 发布于 2020-02-28
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
MySQL 查看运行的是哪份配置文件阅读 1699
-
JVM:垃圾标记阶段算法之可达性分析法阅读 558
-
Prometheus+Grafana+node_exporter 监控 Linux 服务器阅读 1417
-
IDEA Debug 人为修改数值模拟数据不一致情况阅读 1068
-
Linux 恢复删除的文件阅读 5038
-
IDEA Debug 时模拟抛出异常阅读 1533
-
RabbitMQ 重启命令阅读 8053
-
Ubuntu 安装 Docker阅读 1277
-
Android ActivityManager阅读 3191
-
Kubernetes 网络代理工具 Telepresence 安装及升级阅读 497