openresty lua-resty-template页面静态化


openresty lua-resty-template页面静态化

         

官网:https://github.com/bungle/lua-resty-template

             

                    

                                     

template 安装

        

lua-resty-template安装

# 查找模块
[root@a8f5f9001e83 bin]#opm search lua-resty-template
bungle/lua-resty-template                         Templating Engine (HTML) for Lua and OpenResty
bungle/lua-resty-validation                       Validation Library (Input Validation and Filtering) for Lua and OpenResty
bungle/lua-resty-session                          Session Library for OpenResty - Flexible and Secure
bungle/lua-resty-nettle                           Nettle (a low-level cryptographic library) Bindings for LuaJIT FFI
d80x86/lua-resty-tofu                             (alpha) a modern api/web framework
DevonStrawn/lua-resty-route                       URL Routing Library for OpenResty Supporting Pluggable Matching Engines
hamishforbes/lua-resty-qless-web                  Port of Moz's qless web interface to the Openresty environment.

# 安装模块
[root@a8f5f9001e83 bin]# opm install 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  92474      0 --:--:-- --:--:-- --:--:-- 92302
Package bungle/lua-resty-template 2.0 installed successfully under /usr/local/openresty/site/ .

# 查看安装的模块
[root@a8f5f9001e83 bin]# opm list
bungle/lua-resty-template                                    2.0

          

              

                                     

template 语法

        

输出内容

{
  {expression}}   # 输出属性
{*expression*}   # 输出属性、导入文件

# 示例
local template = require "resty.template"
template.render("view.html", {
  message = "Hello, World!",
})

## view.html
<h1>{
  {message}}</h1>   -- 输出message内容

         

导入模版文件

{(template)}   
{[expression]}

# 示例
{(header.html)}
{(file.html, { message = "Hello, World" } )}

{[file.html]}
{["file.html", { message = "Hello, World" } ]}

{(view.html)}
{(users/list.html)}   -- list.html在users目录下

         

执行lua代码

{% lua code %} 

# 示例
{(header.html)}
<h1>{
  {message}}</h1>
<ul>
{% for _, name in ipairs(names) do %}     -- lua代码
    <li>{
  {name}}</li>
{% end %}                                 -- lua代码
</ul>
{(footer.html)}

              

定义语句块,可将语句块导入其他文件

{-block-}...{-block-}

# 示例
local view     = template.new("view.html", "layout.html")
view.title     = "Testing lua-resty-template blocks"
view.message   = "Hello, World!"
view.keywords  = { "test", "lua", "template", "blocks" }
view:render()

## view.html
<h1>{
  {message}}</h1>
{-aside-}              -- 定义语句块
<ul>
    {% for _, keyword in ipairs(keywords) do %}
    <li>{
  {keyword}}</li>
    {% end %}
</ul>
{-aside-}

## layout.html
<!DOCTYPE html>
<html>
<head>
<title>{*title*}</title>
</head>
<body>
<article>
    {*view*}           -- 导入模板文件
</article>
{% if blocks.aside then %}
<aside>
    {*blocks.aside*}   -- 导入语句块
</aside>
{% end %}
</body>
</html>


==> 输出内容

<!DOCTYPE html>
<html>
<head>
<title>Testing lua-resty-template blocks</title>
</head>
<body>
<article>
    <h1>Hello, World!</h1>
</article>
<aside>
    <ul>
        <li>test</li>
        <li>lua</li>
        <li>template</li>
        <li>blocks</li>
    </ul>
</aside>
</body>
</html>

          

注释

{# comments #}

# 示例 
{# {*["foo:bar"]*} won't work, you need to use: #}   -- 注释
{*context["foo:bar"]*}       --输出context中key为["foo:bar"]的内容

         

转义字符

# 不输出message内容
<h1>\{
  {message}}</h1>    
==>  <h1>{
  {message}}</h1>,不会输出message的值

# 输出转义字符
<h1>\\{
  {message}}</h1>   
==>  <h1>\message_value </h1>

           

context 上下文

# 两者等价
<h1>{
  {message}}</h1> == <h1>{
  {context.message}}</h1>


# 特殊格式处理
local ctx = {["foo:bar"] = "foobar"}

{# {*["foo:bar"]*} won't work, you need to use: #}
{*context["foo:bar"]*}


# context中不建议设置的key
___、context、echo、include
layout、blocks、template、render

         

               

                                     

配置指令

        

模板文件目录

template_root       ==>    set $template_root /templates
template_location   ==>    set $template_location /templates


# lua-resty-template 2.x 设置root、location
local template = require "resty.template".new({
  root     = "/templates",
  location = "/templates" 
})


If none of these are set in Nginx configuration, 
ngx.var.document_root (aka root-directive) value is used. 
* template_root、template_location都没有设置,使用root目录

If template_location is set, it will be used first, and if 
the location returns anything but 200 as a status code, we do 
fallback to either template_root (if defined) or document_root
* 如果设置了template_location,优先使用template_location
* 如果template_location没有找到模版,使用template_root、root目录

             

示例:使用项目根路径

http {
  server {
    location / {

      root html;

      content_by_lua '
        local template = require "resty.template"
        template.render("view.html", { message = "Hello, World!" })
      ';      
    }
  }
}

           

示例:使用template_root

http {
  server {

    set $template_root /usr/local/openresty/nginx/html/templates;

    location / {
      root html;
      content_by_lua '
        local template = require "resty.template"
        template.render("view.html", { message = "Hello, World!" })
      ';      
    }
  }
}<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值