openresty模板html页面,OpenResty 中模板渲染引擎 lua-resty-template

项目介绍

lua-resty-template是Lua和OpenResty中的模板渲染引擎。由Kong网关核心工程师bungle开源,京东商城使用此项目完成相关开发。

快速入门

安装

luarocks install lua-resty-template

content_by_lua_file

local template = require "resty.template"

template.render("post.html", { message = "Hello, Post!" })

post.html

{{message}}

输出

Hello, World!

模板语法

{{expression}}:输出传递的值,转义html相关标签

{*expression*}:输出传递的值

{% lua code %}:使用lua代码

{(template)}:引入html共用页面

{(base.html, { title = "Hello, World" } )}:引入html共用页面,并传递相关值

{-verbatim-}...{-verbatim-}/{-raw-}...{-raw-}:可原样输出模板语法

{# comments #}:在模板中使用注释,不会被执行和输出

简要示例

{{expression}}

message = "

Hello World

"

输出:

Hello World

{{message}}

{*expression*}

message = "

Hello World

"

输出:Hello World(带h1样式)

{*message*}

{% lua code %}

使用if判断。

{% if 1 == 2 then %}

1 = 2

{% else %}

1 ~= 2

{% 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}} | 程序员技术之旅

{-raw-}...{-raw-}

可原样输出lua-resty-template中的语法。

输出:{{message}}。

{-raw-}{{message}}{-raw-}

{# comments #}

模板中注释,帮助开发者记录,不会被执行和输出。

内容123

{# This is comment #}

内容456

开源案例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值