前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenResty实战系列 | HTML模板引擎库 lua-testy-template

OpenResty实战系列 | HTML模板引擎库 lua-testy-template

作者头像
Tinywan
发布2024-08-05 14:29:29
1000
发布2024-08-05 14:29:29
举报
文章被收录于专栏:开源技术小栈

简介

Lua-testy-template 是一个基于OpenResty(一个强大的Nginx与Lua的集成)的轻量级模板引擎。它的设计目标是为了解决在Nginx环境中快速生成动态HTML页面的需求,提供简单、高效且灵活的模板处理能力。

Lua-testy-templat 采用了类似 ERB (Ruby) 或 EJS (JavaScript) 的标签语法,使得HTML代码与Lua逻辑相融合。这种设计允许开发者在不脱离HTML上下文的情况下进行数据处理和控制流操作。

应用场景

  • Web服务后端渲染:在Nginx上直接处理HTTP请求,生成动态HTML,降低服务器负载。
  • API Gateway:结合OpenResty的强大能力,可以用于构建复杂的API网关,将部分逻辑移至前端之前完成。
  • 静态站点生成:虽然主要用于动态渲染,但在一些简化的场景下,也可以作为静态站点生成工具。

安装

这里通过OPM工具包安装,更多请查看OpenResty实战系列 | 包管理工具OPM和LuaRocks

代码语言:javascript
复制
#opm get bungle/lua-resty-template


* Fetching bungle/lua-resty-template  
  Downloading https://opm.openresty.org/api/pkg/tarball/bungle/lua-resty-template-2.0.opm.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 40890  100 40890    0     0  27987      0  0:00:01  0:00:01 --:--:-- 27987
Package bungle/lua-resty-template 2.0 installed successfully under /usr/local/openresty/site/ .

版本信息

代码语言:javascript
复制
# opm info bungle/lua-resty-template
Name             : lua-resty-template
Version          : 2.0
Abstract         : Templating Engine (HTML) for Lua and OpenResty
Author           : Aapo Talvensaari (@bungle)
Account          : bungle
Code Repo        : https://github.com/bungle/lua-resty-template
License          : BSD 3-Clause "New" or "Revised" license
Original Work    : yes

配置

默认模板文件存放路径在ngx.var.document_root路径。如果需要自定义,可以通过以下参数定义

代码语言:javascript
复制
template_root (set $template_root /var/www/site/templates)
template_location (set $template_location /templates)

如果在Nginx配置中没有这些设置,则使用ngx.var.document_root的值。如果设置了template_location,并且正常返回(状态码200),则使用其渲染。如果找不到,将回溯到template_rootdocument_root

想知道ngx.var.document_root是什么,可以尝试打印看看

代码语言:javascript
复制
ngx.say(ngx.var.document_root)
ngx.say(ngx.var.template_root)

以上打印输出

代码语言:javascript
复制
/usr/local/openresty/nginx/html  -- 默认
/usr/local/openresty/nginx/conf/lua/view -- 新配置路径

基础使用

使用 lua-resty-template 渲染一个简单html基本示例

hello_template.lua文件

代码语言:javascript
复制
local template = require "resty.template"      -- OR
-- local template = require "resty.template.safe" -- return nil, err on errors
-- Using template.new
local view = template.new "hello.html"
view.message = "Hello, World!"
view:render()

或者

代码语言:javascript
复制
local template = require "resty.template"
local view = template.new "hello.html"
view.message = "Hello, World!"
-- Using template.render
template.render("hello.html", { message = "Hello, World!" })

hello.html

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>开源技术小栈</title>
</head>
<body>
  <h1>{{message}}</h1>
</body>
</html>

openresty.tinywan.com.conf配置文件

代码语言:javascript
复制
server {
    listen 80;
    server_name openresty.tinywan.com;

    set $template_root /usr/local/openresty/nginx/conf/lua/view;
    location /resty_template {
        default_type "text/html";
        lua_code_cache off; 
        content_by_lua_file conf/lua/hello_template.lua;
    }
}

请求访问 http://openresty.tinywan.com/resty_template

同样的事情也可以用inline template string来做

代码语言:javascript
复制
local template = require "resty.template"      -- OR
local view = template.new "hello.html"
view.message = "Hello, World!"
template.render([[
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>开源技术小栈</title>
</head>
<body>
    <h1>{{message}}</h1>
</body>
</html>]], { message = "Hello, 开源技术小栈!" })

模板语法

您可以在模板中使用以下标签

  • {{expression}},写入表达式的结果- html转义
  • {*expression*},写入表达式的结果
  • {% lua code %},执行Lua code
  • {(template)},包含模板文件,您也可以为包含文件{(file.html,{ message =“Hello,World”})}提供上下文(注意:您不能在file.html中使用逗号(),在这种情况下,请使用{[“file,with,comma”]}代替)
  • {[expression]},包含表达式文件(表达式的结果),您还可以为包含文件{[“file.html”,{ message =“Hello,World”} ]}提供上下文
  • {-block-}. {-block-},在{-block-}内部包装为存储在具有键块的块表中的值(在本例中),请参见使用块。不要逐字原始地使用预定义的块名。
  • {-逐字-}... {-逐字-}{-原始-}. {-raw-}是预定义的块,其内部不被lua-resty-template处理,但内容按原样输出。
  • {# comments #}``{##}之间的所有内容都被认为是注释掉的(即不输出或执行)

从模板中,您可以访问上下文表中的所有内容,以及模板表中的所有内容。在模板中,您还可以通过前缀键访问上下文模板

高级使用

代码语言:javascript
复制
local template = require "resty.template" 

-- get var live_id
local live_id = ngx.var.live_id

-- 成员数组
members = { Tom = 2020, Jake = 2024, Dodo = 2028, Jhon = 2030 }
template.render("hello.html", { 
    title = "开源技术小栈",
    live_id = live_id,
    members = members 
})

hello.html 渲染文件

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>开源技术小栈</title>
</head>
<body>
<h1>{{title}} {{live_id}}</h1>
<ul>
    {% for value, name in pairs(members) do %}
    <li>{{value}} == {{name}}</li>
    {% end %}
</ul>
</body>
</html>

请求访问渲染:http://openresty.tinywan.com/resty_template

更多:https://github.com/bungle/lua-resty-template

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-08-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 开源技术小栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 应用场景
  • 安装
  • 配置
  • 基础使用
  • 模板语法
  • 高级使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档