<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 簡介 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](https://mp.weixin.qq.com/s/s6-fukvBWdMuYRDa-cYZHw) ```lua #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/ . ``` 版本信息 ```lua # 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`路徑。如果需要自定義,可以通過以下參數定義 ``` template_root (set $template_root /var/www/site/templates) template_location (set $template_location /templates) ``` 如果在Nginx配置中沒有這些設置,則使用`ngx.var.document_root`的值。 如果設置了`template_location`,并且正常返回(狀態碼200),則使用其渲染。如果找不到,將回溯到`template_root`或`document_root`。 想知道`ngx.var.document_root`是什么,可以嘗試打印看看 ``` ngx.say(ngx.var.document_root) ngx.say(ngx.var.template_root) ``` 以上打印輸出 ``` /usr/local/openresty/nginx/html -- 默認 /usr/local/openresty/nginx/conf/lua/view -- 新配置路徑 ``` ## 基礎使用 使用 lua-resty-template 渲染一個簡單html基本示例 `hello_template.lua`文件 ```lua 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() ``` 或者 ``` 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` ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>開源技術小棧</title> </head> <body> <h1>{{message}}</h1> </body> </html> ``` `openresty.tinywan.com.conf`配置文件 ``` 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 ![](https://img.kancloud.cn/75/04/75041fb6c59ab9e8fcebc71813db7e6d_692x258.png) 同樣的事情也可以用`inline template string`來做 ``` 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, 開源技術小棧!" }) ``` ![](https://img.kancloud.cn/94/7d/947d243004f88390242fe71e98e37f3d_860x281.png) ## 模板語法 您可以在模板中使用以下標簽 * `{{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-}`內部包裝為存儲在具有鍵塊的塊表中的值(在本例中),請參見[使用塊](https://github.com/bungle/lua-resty-template#using-blocks)。不要`逐字`和`原始地`使用預定義的塊名。 * `{-逐字-}... {-逐字-}`和`{-原始-}. {-raw-}`是預定義的塊,其內部不被`lua-resty-template`處理,但內容按原樣輸出。 * `{# comments #}``{#`和`#}`之間的所有內容都被認為是注釋掉的(即不輸出或執行) 從模板中,您可以訪問`上下文`表中的所有內容,以及`模板`表中的所有內容。在模板中,您還可以通過前綴鍵訪問`上下文`和`模板`。 ## 高級使用 ``` 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` 渲染文件 ``` <!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://img.kancloud.cn/b8/d1/b8d1f89487a4bf42533b3a09451f7344_860x516.png) 更多:https://github.com/bungle/lua-resty-template
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看