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>
开源案例
阅读 5253 · 发布于 2020-02-28
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
软考-系统架构设计师:分布式数据库阅读 1241
-
Windows 查看是否是固态硬盘阅读 1338
-
Windows 安装包管理器 choco阅读 1081
-
Vue No module factory available for dependency type: CssDependency阅读 56
-
JavaScript 判断 Android 还是 iOS阅读 474
-
Spring Boot 整合 Log4j2 不起作用阅读 1408
-
Vue 脚手架工具 Vue CLI阅读 59
-
Android 使用 adb wm 命令调整手机分辨率阅读 23031
-
Spring Boot Controller 路径匹配规则阅读 1144
-
IDEA 多个 Vue.js 工程识别 Webpack @ 别名阅读 321