22_openresty使用MVC模板


title: openresty使用MVC模板
urlname: 22_openresty使用MVC模板
author: vsyour
language: zh-CN
date: 2017-05-04 03:03:03
tags: [openresty,lua]
categories: 网络安全

openresty使用MVC模板
我是用默认路径安装了openresty.
就是直接用.configure&&make&&make install
安装完成的.其他的什么都不改.

官方说明:https://github.com/bungle/lua-resty-template
lua-resty-template

下载安装

LuaRocks安装
luarocks install lua-resty-template

如果没有luarocks可以用yum或者apt安装一个

配置nginx

设置两个变量就行了:
template_root
放在server {下面

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

template_location
放到location / {下面

set $template_location /templates

配置方式一

用Using document_root的方法配置:

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

配置方式二(推荐)

用Using 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!" })
      ';      
    }
  }
}

配置方式三

用Using template_location的方法配

http {
  server {
    set $template_location /templates;
    location / {
      root html;
      content_by_lua '
        local template = require "resty.template"
        template.render("view.html", { message = "Hello, World!" })
      ';      
    }
    location /templates {
      internal;
      alias html/templates/;
    }    
  }
}
mkdir -p /usr/local/openresty/nginx/html/templates
cd /usr/local/openresty/nginx/html/templates
vi view.html

文件内容如下:

<!DOCTYPE html>
<html>
<body>
  <h1>{{message}}</h1>
</body>
</html>

这样就可以用模板操作了.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ou6AqbIX-1680156412325)(null)]

{{message}}

``` 这样就可以用模板操作了. [外链图片转存中...(img-1M970tVx-1680156409368)] QQ群:397745473
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vsyour

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值