模版渲染

动态web网页开发是Web开发中一个常见的场景,比如像京东商品详情页,其页面逻辑是非常复杂的,需要使用模板技术来实现。而Lua中也有许多模板引擎,如目前京东在使用的lua-resty-template,可以渲染很复杂的页面,借助LuaJIT其性能也是可以接受的。



如果学习过JavaEE中的servlet和JSP的话,应该知道JSP模板最终会被翻译成Servlet来执行;而lua-resty-template模板引擎可以认为是JSP,其最终会被翻译成Lua代码,然后通过ngx.print输出。



而lua-resty-template和大多数模板引擎是类似的,大体内容有:

模板位置:从哪里查找模板;

变量输出/转义:变量值输出;

代码片段:执行代码片段,完成如if/else、for等复杂逻辑,调用对象函数/方法;

注释:解释代码片段含义;

include:包含另一个模板片段;

其他:lua-resty-template还提供了不需要解析片段、简单布局、可复用的代码块、宏指令等支持。

下载lua-resty-template

wget https://github.com/bungle/lua-resty-template/archive/v1.9.tar.gz
tar -xvzf v1.9.tar.gz

解压后可以看到lib/resty下面有一个template.lua,这个就是我们所需要的,在template目录中还有两个lua文件,将这两个文件复制到/usr/openResty/lualib/resty中即可。

接下来就可以通过如下代码片段引用了:

local template = require("resty.template")

模版位置

我们需要告诉lua-resty-template去哪儿加载我们的模块,此处可以通过set指令定义template_location、template_root或者从root指令定义的位置加载。

我们可以在openResty.conf配置文件的server部分定义

# first match ngx location
set $template_location "/templates";
# then match root read file
set $template_root "/usr/openResty/templates";

也可以通过在server部分定义root指令

root /usr/openResty/templates;

其顺序是

local function load_ngx(path)
local file, location = path, ngx_var.template_location
if file:sub(1) == "/" then file = file:sub(2) end
if location and location ~= "" then
if location:sub(-1) == "/" then location = location:sub(1, -2) end
local res = ngx_capture(location .. '/' .. file)
if res.status == 200 then return res.body end
end
local root = ngx_var.template_root or ngx_var.document_root
if root:sub(-1) == "/" then root = root:sub(1, -2) end
return read_file(root .. "/" .. file) or path
end
  1. 通过ngx.location.capture从template_location查找,如果找到(状态为为200)则使用该内容作为模板;此种方式是一种动态获取模板方式;
  2. 如果定义了template_root,则从该位置通过读取文件的方式加载模板;
  3. 如果没有定义template_root,则默认从root指令定义的document_root处加载模板。



    此处建议首先template_root,如果实在有问题再使用template_location,尽量不要通过root指令定义的document_root加载,因为其本身的含义不是给本模板引擎使用的。



    接下来定义模板位置
mkdir /usr/openResty/templates
mkdir /usr/openResty/templates2

openResty.conf配置文件

# first match ngx location
set $template_location "/templates";
# then match root read file
set $template_root "/usr/openResty/templates"; location /templates {
internal;
alias /usr/openResty/templates2;
}

首先查找/usr/openResty/template2,找不到会查找/usr/openResty/templates。

然后创建两个模板文件

vi /usr/openResty/templates2/t1.html

内容为

template2
vi /usr/example/templates/t1.html

内容为

 template1

test_temlate_1.lua

local template = require("resty.template")
template.render("t1.html")

openResty.conf配置文件

location /lua_template_1 {
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/openResty/lua/test_template_1.lua;
}

访问如http://127.0.0.1/lua_template_1将看到template2输出。然后rm /usr/openResty/templates2/t1.html,reload nginx将看到template1输出。

接下来的测试我们会把模板文件都放到/usr/openResty/templates下。

API

使用模板引擎目的就是输出响应内容;主要用法两种:直接通过ngx.print输出或者得到模板渲染之后的内容按照想要的规则输出。

test_template_2.lua

local template = require("resty.template")
--是否缓存解析后的模板,默认true
template.caching(true)
--渲染模板需要的上下文(数据)
local context = {title = "title"}
--渲染模板
template.render("t1.html", context) ngx.say("<br/>")
--编译得到一个lua函数
local func = template.compile("t1.html")
--执行函数,得到渲染之后的内容
local content = func(context)
--通过ngx API输出
ngx.say(content)

常见用法即如下两种方式:要么直接将模板内容直接作为响应输出,要么得到渲染后的内容然后按照想要的规则输出。

openResty.conf配置文件

location /lua_template_2 {
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/openResty/lua/test_template_2.lua;
}

使用示例

test_template_3.lua

local template = require("resty.template")

local context = {
title = "测试",
name = "张三",
description = "<script>alert(1);</script>",
age = 20,
hobby = {"电影", "音乐", "阅读"},
score = {语文 = 90, 数学 = 80, 英语 = 70},
score2 = {
{name = "语文", score = 90},
{name = "数学", score = 80},
{name = "英语", score = 70},
}
} template.render("t3.html", context)

模板文件/usr/openResty/templates/t3.html

<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
{# 不转义变量输出 #}
姓名:{* string.upper(name) *}<br/>
{# 转义变量输出 #}
简介:{{description}}<br/>
{# 可以做一些运算 #}
年龄: {* age + 1 *}<br/>
{# 循环输出 #}
爱好:
{% for i, v in ipairs(hobby) do %}
{% if i > 1 then %},{% end %}
{* v *}
{% end %}<br/> 成绩:
{% local i = 1; %}
{% for k, v in pairs(score) do %}
{% if i > 1 then %},{% end %}
{* k *} = {* v *}
{% i = i + 1 %}
{% end %}<br/>
成绩2:
{% for i = 1, #score2 do local t = score2[i] %}
{% if i > 1 then %},{% end %}
{* t.name *} = {* t.score *}
{% end %}<br/>
{# 中间内容不解析 #}
{-raw-}{(file)}{-raw-}
</body>
</html>

{(include_file)}:包含另一个模板文件;

{* var *}:变量输出;

{{ var }}:变量转义输出;

{% code %}:代码片段;

{# comment #}:注释;

{-raw-}:中间的内容不会解析,作为纯文本输出;

模板最终被转换为Lua代码进行执行,所以模板中可以执行任意Lua代码。

openResty.conf配置文件

location /lua_template_3 {
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/openResty/lua/test_template_3.lua;
}

访问如http://127.0.0.1/lua_template_3进行测试。

基本的模板引擎使用到此就介绍完了。

高并发 Nginx+Lua OpenResty系列(8)——Lua模版渲染的更多相关文章

  1. 高并发 Nginx+Lua OpenResty系列(11)——流量复制/AB测试/协程

    流量复制 在实际开发中经常涉及到项目的升级,而该升级不能简单的上线就完事了,需要验证该升级是否兼容老的上线,因此可能需要并行运行两个项目一段时间进行数据比对和校验,待没问题后再进行上线.这其实就需要进 ...

  2. 高并发 Nginx+Lua OpenResty系列(4)——Lua 模块开发

    在实际开发中,不可能把所有代码写到一个大而全的lua文件中,需要进行分模块开发:而且模块化是高性能Lua应用的关键.使用require第一次导入模块后,所有Nginx 进程全局共享模块的数据和代码,每 ...

  3. 高并发 Nginx+Lua OpenResty系列(2)——Nginx Lua API

    Nginx Lua API 和一般的Web Server类似,我们需要接收请求.处理并输出响应.而对于请求我们需要获取如请求参数.请求头.Body体等信息:而对于处理就是调用相应的Lua代码即可:输出 ...

  4. 高并发 Nginx+Lua OpenResty系列(1)——环境搭建

    OpenResty是一款基于Nginx的高性能负载均衡服务器容器,简单来说是Nginx+Lua.结合了Lua语言来对Nginx进行扩展,使得在Nginx上具有web容器功能. OpenResty运行环 ...

  5. 高并发 Nginx+Lua OpenResty系列(7)——Lua开发库json

    JSON库 在进行数据传输时JSON格式目前应用广泛,因此从Lua对象与JSON字符串之间相互转换是一个非常常见的功能:目前Lua也有几个JSON库,如:cjson.dkjson.其中cjson的语法 ...

  6. 高并发 Nginx+Lua OpenResty系列(10)——商品详情页

    本章以京东商品详情页为例,京东商品详情页虽然仅是单个页面,但是其数据聚合源是非常多的,除了一些实时性要求比较高的如价格.库存.服务支持等通过AJAX异步加载加载之外,其他的数据都是在后端做数据聚合然后 ...

  7. 高并发 Nginx+Lua OpenResty系列(9)——HTTP服务

    此处我说的HTTP服务主要指如访问京东网站时我们看到的热门搜索.用户登录.实时价格.实时库存.服务支持.广告语等这种非Web页面,而是在Web页面中异步加载的相关数据.这些服务有个特点即访问量巨大.逻 ...

  8. 高并发 Nginx+Lua OpenResty系列(5)——Lua开发库Redis

    Redis客户端 lua-resty-redis是为基于cosocket API的ngx_lua提供的Lua redis客户端,通过它可以完成Redis的操作.默认安装OpenResty时已经自带了该 ...

  9. 高并发 Nginx+Lua OpenResty系列(3)——模块指令

    Nginx Lua 模块指令 Nginx共11个处理阶段,而相应的处理阶段是可以做插入式处理,即可插拔式架构:另外指令可以在http.server.server if.location.locatio ...

随机推荐

  1. Linux经常使用的命令(两) - cd

    Linux cd 命令能够说是Linux中最主要的命令语句,其它的命令语句要进行操作,都是建立在使用 cd 命令上的.所以,学习Linux 经常使用命令.首先就要学好 cd 命令的用法技巧. 1. 命 ...

  2. 回归(regression)的理解(regressor,回归子)

    1. 基本概念 回归(regression)是监督学习(given {(xi,yi)})的一个重要分类.回归用于预测输入变量(自变量,Xi)与输出变量(因变量,Yi) 之间的关系,特定是当输入变量的值 ...

  3. 外部进程嵌入到Qt进程界面(使用QWindow::fromWinId)

    有时候需要做框架集成的东西,需要把其他客户端像组件一样集成到一个客户端中,类似于一个软件集成的平台客户端,统一用一个中心管理的客户端做类似于控制面板一样的东西去调用不同的软件.此时就必须相应不同的点击 ...

  4. 在.net MVC项目中使用ajax进行数据验证

    1.首先要在网页引入应该引入的js文件 在这里回顾一下在模板页里面挖坑的技术 2.在html中使用html辅助方法 3.验证模型 4验证方法

  5. 两个QWidget叠加,可部分代替layout的功能

    在QT开发过程中,有时候会遇到这样的问题,当我们自己创建了一个Layout对象以后,使用QWidget的setLayout方法,将这个Layout对象应用到窗口中的时候,发现窗口上没有我们添加的控件, ...

  6. Python类型和对象

    关键字:Python 类型 对象原文:http://wiki.woodpecker.org.cn/moin/PyTypesAndObjects 关于本书 解释新式的Python对象(new-style ...

  7. Android在putString和getString使用方法

    函数: putString(String key,String value); 功能:将键为key的值为value. 详细的容器详细考虑比如对于Editor 的对象来讲: 代码例如以下: 首先新建一个 ...

  8. C#实现进程内存信息获取

    using System.Collections.Generic;using System.Runtime.InteropServices;using System;using System.Diag ...

  9. 学习git命令

    1.git init @创建仓库 2.git add  filename @添加文件到缓存区 3.git commit -m"注释说明"   @提交修改内容 4.git statu ...

  10. VC绘制控件如何防止闪烁

    理论上不管什么控件都适用,方法如下: 新建一个MFC类,继承原来的控件类型,对于VC自动生成的类进行如下改动: 首先覆盖 OnEraseBkgnd() 防止擦除时填涂背景: BOOL CXXXCtrl ...