Code Monkey home page Code Monkey logo

lua-resty-template's People

Contributors

bungle avatar cuiweixie avatar decadence avatar jason-cooke avatar patrickdahlin avatar xiangnanscu avatar zhoukk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lua-resty-template's Issues

Can `layout = 'foo.html'` be replaced by `local layout = 'foo.html'`?

Hello, bungle, I notice the examples in README.md which show defining layout in the file:

{% layout="section.html" %}
<h1>{{message}}</h1>

can be added a local, the output is the same

{% local layout="section.html" %}
<h1>{{message}}</h1>

I just wondering:
Is {% layout="section.html" %} same to {% local layout="section.html" %} ?

broken link in README.md

Hi community. I found some broken links in README with https://github.com/tcort/markdown-link-check

[✖] http://turbolua.org/doc/web.html#mustache-templating
[✖] http://stevedonovan.github.io/Penlight/api/modules/pl.template.html
[✖] http://doc.lubyk.org/lub.Template.html
[✖] https://github.com/torhve/LuaWeb/blob/master/tirtemplate.lua
[✖] http://fperrad.github.io/lua-CodeGen/
[✖] http://luasp.org/
[✖] https://bitbucket.org/sphen_lee/fraudster
[✖] https://github.com/tgn14/Lua-template

模版错误,较难排错

错误输出,全部,是如下样子,
很难找模版那里错误了.
请问本来这样,还是要其他方式排错呢

2016/02/03 13:36:40 [error] 25868#0: *1 lua entry thread aborted: runtime error: [string "context=... or {}..."]:101: attempt to index a nil value
stack traceback:
coroutine 0:
[string "context=... or {}..."]: in main chunk

performance tests between lua-resty-template and others

Hi,

We are looking into selecting a template engine for our coming LUA applications. We been now reading between your module and etlua. If I understood right both are doing the same.

Is it possible for future to consider adding some comparative test results for resty-template vs etlua or others ... or a section for README regarding performance consideration regarding resty-template ?

Thanks

Template is not rendered using example from README

I installed nginx, lua and luarocks and used luarocks to install lua-resty-template.
Using the simple example shown in the README, my page only shows the text "view.html" instead of rendering the contents of the view.html file.
I built a clean environment with current source releases, static linked, on alpine linux.
I get no errors with nginx on debug logging.
Am I missing something, or is the README?

angularjs template error,can super the grammar

function </Volumes/osxwork/work/nginx_web/lua/apkupdate.lua:1>, client: 127.0.0.1, server: , request: "GET /file-upload.html HTTP/1.1", host: "127.0.0.1:8088"
2016/02/17 17:40:15 [error] 46316#0: *202 lua entry thread aborted: runtime error: /Volumes/osxwork/work/nginx_web/lua/resty/template.lua:178: [string "context=(...) or {}..."]:116: ')' expected near '|'
stack traceback:
coroutine 0:
[C]: in function 'load_chunk'
/Volumes/osxwork/work/nginx_web/lua/resty/template.lua:178: in function 'compile'
/Volumes/osxwork/work/nginx_web/lua/resty/template.lua:291: in function 'render'
/Volumes/osxwork/work/nginx_web/lua/apkupdate.lua:49: in function </Volumes/osxwork/work/nginx_web/lua/apkupdate.lua:1>, client: 127.0.0.1, server: , request: "GET /file-upload.html HTTP/1.1", host: "127.0.0.1:8088"


<head>
    <title>Simple example</title>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />

    <!-- Fix for old browsers -->
    <script src="http://nervgh.github.io/js/es5-shim.min.js"></script>
    <script src="http://nervgh.github.io/js/es5-sham.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="../console-sham.js"></script>

    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

    <!--<script src="../bower_components/angular/angular.js"></script>-->
    <script src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
    <script src="http://nervgh.github.io/pages/angular-file-upload/dist/angular-file-upload.min.js"></script>
    <script src="http://nervgh.github.io/pages/angular-file-upload/examples/simple/controllers.js"></script>

    <style>
        .my-drop-zone { border: dotted 3px lightgray; }
        .nv-file-over { border: dotted 3px red; } /* Default class applied to drop zones on over */
        .another-file-over-class { border: dotted 3px green; }

        html, body { height: 100%; }
    </style>

</head>

<!-- 1. nv-file-drop="" uploader="{Object}" options="{Object}" filters="{String}" -->
<body ng-controller="AppController" nv-file-drop="" uploader="uploader" filters="queueLimit, customFilter">

    <div class="container">

        <div class="navbar navbar-default">
            <div class="navbar-header">
                <a class="navbar-brand" href="https://github.com/nervgh/angular-file-upload">Angular File Upload</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li class="active dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Demos <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li class="active"><a href="#">Simple example</a></li>
                            <li><a href="../image-preview">Uploads only images (with canvas preview)</a></li>
                            <li><a href="../without-bootstrap">Without bootstrap example</a></li>
                        </ul>
                    </li>
                    <li><a href="https://github.com/nervgh/angular-file-upload">View on Github</a></li>
                    <li><a href="https://raw.githubusercontent.com/nervgh/angular-file-upload/master/dist/angular-file-upload.min.js">Download</a></li>
                </ul>
            </div>
        </div>

        <div class="row">

            <div class="col-md-3">

                <h3>Select files</h3>

                <div ng-show="uploader.isHTML5">
                    <!-- 3. nv-file-over uploader="link" over-class="className" -->
                    <div class="well my-drop-zone" nv-file-over="" uploader="uploader">
                        Base drop zone
                    </div>

                    <!-- Example: nv-file-drop="" uploader="{Object}" options="{Object}" filters="{String}" -->
                    <div nv-file-drop="" uploader="uploader" options="{ url: '/foo' }">
                        <div nv-file-over="" uploader="uploader" over-class="another-file-over-class" class="well my-drop-zone">
                            Another drop zone with its own settings
                        </div>
                    </div>
                </div>

                <!-- Example: nv-file-select="" uploader="{Object}" options="{Object}" filters="{String}" -->
                Multiple
                <input type="file" nv-file-select="" uploader="uploader" multiple  /><br/>

                Single
                <input type="file" nv-file-select="" uploader="uploader" />
            </div>

            <div class="col-md-9" style="margin-bottom: 40px">

                <h3>Upload queue</h3>
                <p>Queue length: {{ uploader.queue.length }}</p>

                <table class="table">
                    <thead>
                        <tr>
                            <th width="50%">Name</th>
                            <th ng-show="uploader.isHTML5">Size</th>
                            <th ng-show="uploader.isHTML5">Progress</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="item in uploader.queue">
                            <td><strong>{{ item.file.name }}</strong></td>
                            <td ng-show="uploader.isHTML5" nowrap>{{ item.file.size/1024/1024|number:2 }} MB</td>
                            <td ng-show="uploader.isHTML5">
                                <div class="progress" style="margin-bottom: 0;">
                                    <div class="progress-bar" role="progressbar" ng-style="{ 'width': item.progress + '%' }"></div>
                                </div>
                            </td>
                            <td class="text-center">
                                <span ng-show="item.isSuccess"><i class="glyphicon glyphicon-ok"></i></span>
                                <span ng-show="item.isCancel"><i class="glyphicon glyphicon-ban-circle"></i></span>
                                <span ng-show="item.isError"><i class="glyphicon glyphicon-remove"></i></span>
                            </td>
                            <td nowrap>
                                <button type="button" class="btn btn-success btn-xs" ng-click="item.upload()" ng-disabled="item.isReady || item.isUploading || item.isSuccess">
                                    <span class="glyphicon glyphicon-upload"></span> Upload
                                </button>
                                <button type="button" class="btn btn-warning btn-xs" ng-click="item.cancel()" ng-disabled="!item.isUploading">
                                    <span class="glyphicon glyphicon-ban-circle"></span> Cancel
                                </button>
                                <button type="button" class="btn btn-danger btn-xs" ng-click="item.remove()">
                                    <span class="glyphicon glyphicon-trash"></span> Remove
                                </button>
                            </td>
                        </tr>
                    </tbody>
                </table>

                <div>
                    <div>
                        Queue progress:
                        <div class="progress" style="">
                            <div class="progress-bar" role="progressbar" ng-style="{ 'width': uploader.progress + '%' }"></div>
                        </div>
                    </div>
                    <button type="button" class="btn btn-success btn-s" ng-click="uploader.uploadAll()" ng-disabled="!uploader.getNotUploadedItems().length">
                        <span class="glyphicon glyphicon-upload"></span> Upload all
                    </button>
                    <button type="button" class="btn btn-warning btn-s" ng-click="uploader.cancelAll()" ng-disabled="!uploader.isUploading">
                        <span class="glyphicon glyphicon-ban-circle"></span> Cancel all
                    </button>
                    <button type="button" class="btn btn-danger btn-s" ng-click="uploader.clearQueue()" ng-disabled="!uploader.queue.length">
                        <span class="glyphicon glyphicon-trash"></span> Remove all
                    </button>
                </div>

            </div>

        </div>

    </div>

</body>

macros or block with parameters

Hi,

Very good template language, but I miss macros or block with parameters.
What I mean by this:

--- my_template.html
<html>
<some><code>
  {* blocks.my_macro(a1, table) *}
</html>

{- my_macro(arg1, arg2) -}
    <div>
      {% if arg1 the %}
           <some>arg2.param1<code>
      {% end %}
    </div>
{-my_macro-}

I tried to call the function lua by {* *} and define the another rendering in lua code, but it does not work properly. eg:

--- my_template.html
<html>
<some><code>
  {* my_macro(a1, table) *}
</html>

my_macro passed in the context as:

template.compile(macro_code)(macro_args)

It seems that the function call template.compile recursively OpenResty is not well tested and does not work properly.

How do I avoid any changes to the message?

My message is a complete HTML table,

<table border=1 class="dataframe"><tr><th>Description</th><th>Day</th><th>Address</th><th>Units</th><th>Daily</th><th>Rate</th><th>Total</th><th>Billed</th><th>DBilled</th><th>TotalBilled</th><th>Credited</th><th>TotalCredited</th><th>Balance</th><th>Count</th><th>ASR</th><th>ACD</th></tr><tr><td>Billed</td><td>2021-03-09</td><td>8.19.245.4</td><td>409.50</td><td>804.00</td><td>0.00439</td><td>807.20</td><td>$1.80</td><td>$3.53</td><td>$3.54</td><td></td><td>$2.00</td><td>$-1.54</td><td>2536</td><td>38.96</td><td>24.87</td></tr></table>
I need to pass it in to the template as is, no changes, instead I get
&lt;table border=1 class=&quot;dataframe&quot;&gt;&lt;tr&gt;&lt;th&gt;Description&lt;&#47;th&gt;&lt;th&gt;Day&lt;&#47;th&gt;&lt;th&gt;Address&lt;&#47;th&gt;&lt;th&gt;Units&lt;&#47;th&gt;&lt;th&gt;Daily&lt;&#47;th&gt;&lt;th&gt;Rate&lt;&#47;th&gt;&lt;th&gt;Total&lt;&#47;th&gt;&lt;th&gt;Billed&lt;&#47;th&gt;&lt;th&gt;DBilled&lt;&#47;th&gt;&lt;th&gt;TotalBilled&lt;&#47;th&gt;&lt;th&gt;Credited&lt;&#47;th&gt;&lt;th&gt;TotalCredited&lt;&#47;th&gt;&lt;th&gt;Balance&lt;&#47;th&gt;&lt;th&gt;Count&lt;&#47;th&gt;&lt;th&gt;ASR&lt;&#47;th&gt;&lt;th&gt;ACD&lt;&#47;th&gt;&lt;&#47;tr&gt;&lt;tr&gt;&lt;td&gt;Billed&lt;&#47;td&gt;&lt;td&gt;2021-03-09&lt;&#47;td&gt;&lt;td&gt;8.19.245.4&lt;&#47;td&gt;&lt;td&gt;409.50&lt;&#47;td&gt;&lt;td&gt;804.00&lt;&#47;td&gt;&lt;td&gt;0.00439&lt;&#47;td&gt;&lt;td&gt;807.20&lt;&#47;td&gt;&lt;td&gt;$1.80&lt;&#47;td&gt;&lt;td&gt;$3.53&lt;&#47;td&gt;&lt;td&gt;$3.54&lt;&#47;td&gt;&lt;td&gt;&lt;&#47;td&gt;&lt;td&gt;$2.00&lt;&#47;td&gt;&lt;td&gt;$-1.54&lt;&#47;td&gt;&lt;td&gt;2536&lt;&#47;td&gt;&lt;td&gt;38.96&lt;&#47;td&gt;&lt;td&gt;24.87&lt;&#47;td&gt;&lt;&#47;tr&gt;&lt;&#47;table&gt;

I tried to use {-raw} but It never gets fixed, or I don't know to use it. How do I integrate a string into the template, verbatim?
Is there any other way to do this?

bad argument #1 to 'load' (function expected, got string)

1, I really love this though i'm fresh to lua.
2, when i run this in my project(lua 5.1), console output error:
com.naef.jnlua.LuaRuntimeException: E:\lua\test\src\template.lua:123: bad argument #1 to 'load' (function expected, got string)
at com.naef.jnlua.LuaState.lua_pcall(Native Method)
at com.naef.jnlua.LuaState.call(LuaState.java:555)

2, so i modify the template.lua :line123, chang "load" to "loadString",save and run it then i get another error:
Exception in thread "main" com.naef.jnlua.LuaRuntimeException: [string "context=... or {}..."]:7: attempt to index global 'template' (a nil value)
at com.naef.jnlua.LuaState.lua_pcall(Native Method)
at com.naef.jnlua.LuaState.call(LuaState.java:555)

3, then i edit the template.lua :line24, remove the modifier "local", then it works .

did i edit the code like this is right ? or it will introduce some unkown error ?

how try catch template runtime exception

lua-resty-template using, if you program have runtime exception I want try catch exception, how to do somthing? for instance catch exception redirect to error page。

link css does not work?

hi, when i use in my html template's tag, the output html does not effect at all, can you help me? BTW, javascript runs ok, with the same root path

Template not displaying page

When on my /servers page:

location /servers {

           root   C:\Users\ianbl\Desktop\Code\WebDev\ServerPanel;
           default_type text/html;
           content_by_lua_block {
               local template = require "resty.template"
               local template = require "resty.template.safe"
               function os.capture(cmd)
                   local f = assert(io.popen(cmd, 'r'))
                   local s = assert(f:read('*a'))
                    f:close()
                    return s
                end

                template.render("servers.html", {
                Name = "ServerPanel",
                IP = "127.0.0.1",
                Status = "Running",
                CPU = "Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz",
                RAM = "8 GB",
                DISK = "SSD"
                })
            }
        }

It just displays a blank page. which is weird because this exact thing used to work just fine, and I don't recall changing anything.
My other pages with identical lua blocks work just fine:

location /login {
            root   C:\Users\ianbl\Desktop\Code\WebDev\ServerPanel;
            default_type text/html;
            content_by_lua_block {
                local template = require "resty.template"      -- OR
                local template = require "resty.template.safe" -- return nil, err on errors
                template.render("login.html", { message = "Hello, World!" })
            }
        }

Does anybody perhaps have a solution? There are no errors in the errors.log, and the HTML code is fine too because it works just fine with the default nginx try_files.

Revise the rule{{}}

When use lua-resty-template with vue.js or AngularJS frame ,{{}} will happen conflict.
For using uniform rules ,change this ({{}}) to {+ +} or {$$} ?

unfriendly traceback on variables redefinition

Hi there! I've discovered, that when I trying to locally (for example, inside for cycle) redefine some global variable (I defined in application earlier) in the template (inside {% %}), then application crashing and I see some "unfriendly" error message:

2014/08/16 11:09:31 [error] 27315#0: *27 lua entry thread aborted: runtime error: [string "context=... or {}..."]:4: attempt to call method 'match' (a nil value)
stack traceback:
coroutine 0:
        [string "context=... or {}..."]: in main chunk
        [string "context=... or {}..."]:8: in function 'r'
        /home/mva/<...>/f/init.lua:9: in function 'render'
        /home/mva/<...>/nginx.lua:44: in function 'start'
        /home/mva/<...>/init.lua:8: in function </home/mva/<...>/init.lua:1>, client: ::1, server: site.local, request: "GET /f HTTP/1.1", host: "site.local", referrer: "http://site.local/"

in the log 😥

Probably, this is somehow related to #2

Rendering with indent?

I'm trying to use lua-resty-template as a simple way to serve up dynamic YAML config files with nginx.

I've got it mostly working but in some cases I need to render other files inside of a template with indentation, but I'd rather not indent the source file because I could be including it at different indent levels.

Any thoughts on how to handle this?

add a pure lua configure method

Hi Aapo,

I'm trying use OpenResty's resty command line tools to test your module, and found I cannot setup a template_location or template_root in the nginx's config file ( or maybe I missed something ).

But, anyway, I think set a pure lua method to setup templete_root is reasonable, and may make the template more easy to use.

Here is my hack, quite ugly but useful for me:

    local root = vars and (var.template_root or var.document_root) or prefix
-- hack: injected lua operated template.tpl_root as template root
    if template.tpl_root then root = template.tpl_root end
-- hack end
    if sub(root, -1) == "/" then root = sub(root, 1, -2) end

and in my code:

local template = require "resty.template"
template.tpl_root = "/some/path/to/template"
template.render("view.html", "hello world")

How do you think this?

Lance

can't use blocks before {*view*} in layout

local template = require "resty.template"
local layout = [[
<div>
  {*blocks.title*}
  {*view*}
  {*blocks.title*}
  {*blocks.title*}
</div>]]
    local content = [[
{-title-}
<div>I'm title</div>
{-title-}
<h1>hello!</h1>]]
ngx.say(template.new(content, layout):process())

output:

<div>
  
  <h1>hello!</h1>
  <div>I'm title</div>
  <div>I'm title</div>
</div>

what I expect is:

<div>
  <div>I'm title</div>
  <h1>hello!</h1>
  <div>I'm title</div>
  <div>I'm title</div>
</div>

Not worked

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

There code in Openresty render only view.html on page

wrong template returned when using multiple server blocks

server {
    server_name a;
    set $template_root /etc/nginx/sites/a;
    location / {
        content_by_lua_file '/etc/nginx/sites/a/index.lua';
    }
}

server {
    server_name b;
    set $template_root /etc/nginx/sites/b;
    location / {
        content_by_lua_file '/etc/nginx/sites/b/index.lua';
    }
}

index.lua:
local template = require "resty.template"
template.render("test.html")

/etc/nginx/sites/a/test.html:
a

/etc/nginx/sites/b/test.html
b

--------------------------------
$ curl https://b/ ; curl https://a/
b
a
$ curl https://b/ ; curl https://a/
b
b <-- wrong

overwriting print() does not work in new()

Hi Aapo,
Thanks for your efforts.

when template.print() overwritten, template.new(..):render(...) does not tail call :

template.print = function(s) return s end

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" }
local a=view:render()

so "a" should be a string with rendered content. But it does not. Have a look at the code(template.lua), should not it be like :

145   return render(layout, context)

or am I missing something ?

Regards

"different template cached" is more slowy than "different template, different context cached"

When i run the bench test like this:

local test = require "resty.template.microbenchmark"; 
test(100000)

The result is :

 Running 100000 iterations in each test
 Parsing Time: 0.612801
 Compilation Time: 2.201334 (template)
 Compilation Time: 0.008615 (template cached)
 Execution Time: 2.655195 (same template)
 Execution Time: 0.350653 (same template cached)
 Execution Time: 15.897813 (different template)
 Execution Time: 7.387499 (different template cached)
 Execution Time: 17.567453 (different template, different context)
 Execution Time: 0.967848 (different template, different context cached)
 Total Time: 47.649211

I found that "different template cached" is more slowy than "different template, different context cached". But i don't know why。

Failed to issue subrequest

I am getting these messages when trying to render the page. The page still renders correctly, though. So it works, just the error is a bit puzzling to me.

I am running this in a Docker container if this helps at all.

Dockerfile

FROM openresty/openresty:alpine-fat

# Install dependencies
RUN opm install openresty/lua-resty-redis
RUN opm install bungle/lua-resty-template

Nginx config file

worker_processes 1;

events {
    worker_connections 1024;
}

http {
  resolver local=on;

  server {
    listen 80;

    # Lua configuration.
    lua_code_cache off;

    # Backend service for pacman.
    location / {
      default_type text/html;
      content_by_lua_file /app/default.lua;
    }
  }
}

Code

local template = require"resty.template".new({
    root = "/app",
    location = "/app"
})

local view = template.new "default.html"
view.message = "Hello, World!"
view:render()
<!DOCTYPE html>
<html>

<head>
    <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
</head>

<body>
    <h1>{{message}}</h1>
</body>

</html>

Output

openresty_1  | 2023/05/17 15:03:36 [error] 7#7: *2 lua subrequests cycle while processing "/app/default.html", client: 172.19.0.1, server: , request: "GET / HTTP/1.1", subrequest: "/app/default.html", host: "localhost:8080"
openresty_1  | 2023/05/17 15:03:36 [error] 7#7: *2 lua entry thread aborted: runtime error: /usr/local/openresty/site/lualib/resty/template.lua:162: failed to issue subrequest: -1
openresty_1  | stack traceback:
openresty_1  | coroutine 0:
openresty_1  | 	[C]: in function 'capture'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:162: in function 'load'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:405: in function 'parse'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:389: in function 'compile'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:637: in function 'process'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:652: in function 'render'
openresty_1  | 	/app/default.lua:8: in main chunk, client: 172.19.0.1, server: , request: "GET / HTTP/1.1", subrequest: "/app/default.html", host: "localhost:8080"
openresty_1  | 172.19.0.1 - - [17/May/2023:15:03:36 +0000] "GET / HTTP/1.1" 200 191 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
openresty_1  | 2023/05/17 15:03:36 [error] 7#7: *2 lua subrequests cycle while processing "/app/default.html", client: 172.19.0.1, server: , request: "GET /favicon.ico HTTP/1.1", subrequest: "/app/default.html", host: "localhost:8080", referrer: "http://localhost:8080/"
openresty_1  | 2023/05/17 15:03:36 [error] 7#7: *2 lua entry thread aborted: runtime error: /usr/local/openresty/site/lualib/resty/template.lua:162: failed to issue subrequest: -1
openresty_1  | stack traceback:
openresty_1  | coroutine 0:
openresty_1  | 	[C]: in function 'capture'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:162: in function 'load'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:405: in function 'parse'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:389: in function 'compile'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:637: in function 'process'
openresty_1  | 	/usr/local/openresty/site/lualib/resty/template.lua:652: in function 'render'
openresty_1  | 	/app/default.lua:8: in main chunk, client: 172.19.0.1, server: , request: "GET /favicon.ico HTTP/1.1", subrequest: "/app/default.html", host: "localhost:8080", referrer: "http://localhost:8080/"
openresty_1  | 172.19.0.1 - - [17/May/2023:15:03:36 +0000] "GET /favicon.ico HTTP/1.1" 200 191 "http://localhost:8080/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"

get correct url in template

I try to add a url from lua-mysql-query result and set in iframe template but got http:\/\/example.com instead http://example.com

lua

local url_to = cjson.encode(res[1]["redirect_to"])
local url_masking = ngx.unescape_uri(url_to)

...

template.render([[
<!DOCTYPE html>
<html>
</head>
<body>

...

<div class="container"> 
  <iframe class="responsive-iframe" src={*masking_to*}></iframe>
</div>

</body>
</html>]], { masking_to = url_masking })
end

...

curl

$ curl hello.com

<div class="container"> 
  <iframe class="responsive-iframe" src="http:\/\/example.com"></iframe>
</div

is there some issue in template or lua ?

child template is rendered even if it is not reachable in the logic of layout

Hello bungle. I define err in layout.html to display exception info.But I find that template engine will render child.html first even if it should not be rendered in layout.html.
I think engine should render layout first then child

lua

template.new('child.html', 'layout.html'):process{err='some exception'}

layout.html

{% if err then %}
  <div class="alert alert-danger">{{err}}</div>
{% else %}
  {*view*}
{% end %}

child.html

<p>{{some_method_cannot_be_called_when_err_happen()}}</p>

Different output with precompiled Lua code

Let's say I have this code in template

// {# comment #}
console.log(name);

Which correctly compiles to

//
console.log(name);

But if I enable precompilation of Lua byte code (content_by_lua_file) it compiles to

// console.log(name);

As you can see bottom line pops up to comment line and this breaks JavaScript code.

As far as I know it happens if you have newline character after comment block without any other characters.

Precompilation: https://blog.openresty.com/en/luajit-bytecode/

i18 / Localization

I've started using this library and I like it. I need a mechanism to support multiple languages (as in i18n/localization). I've done some initial googling but have not found pointers to how to do that so far. Neither with this library nor e.g. in the awesome resty library collection. Any ideas/pointers would be highly appreciated.

Consider grandfather-father-son inheritance.

Dude, I can't use google groups now. So let's talk here. the original post is here.

Actually I am using your lua-resty-template. It does support father-son inheritance. But note my example, it's actually grandfather-father-son inheritance which is not supported if I'm not wrong. for example:

base.html

<html lang='zh'>
   <head>
   <link href="css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>
   {* blocks.content *}
   <script src="js/jquery.js"></script>
   <script src="js/bootstrap.min.js"></script>
   </body>
</html>

home.html

{% layout = "base.html" %}
{-content-}This is Home!{-content-}
{* blocks.addition *}

page.html

{% layout = "home.html" %}
{-addition-}This is addtion..{-addition-}

test.lua

local res = require"resty.template".compile("home.html"){} --this will work as expected
local res = require"resty.template".compile("page.html"){} -- this will not work. additon block will be ignored.

在 2016年6月14日星期二 UTC+8上午5:17:52,Aapo Talvensaari写道:

On Monday, 13 June 2016 04:33:09 UTC+3, 项楠 wrote:

    I'm looking for something similar to the directive `{% extends "base.html" %}` in Jinja2 template engine. I've read the docs of tt2, but `block`,`process`, `include` and `wrapper` are all seems not to be for template inheritance.


I don't mean to hijack this thread, but everything you describe is supported in lua-resty-template:
 https://github.com/bungle/lua-resty-template

Syntax os a bit different though:

1.

{% extends "layout2.html" %}
 =>
{% layout = "layout2.html" %}


2.

{% block nav %}<div class="nav"></div>{% endblock %}
=>
{-nav-}<div class="nav"></div>{-nav-}


3.

{% block nav %} {% endblock %}
=>
{* blocks.nav *}

etc.

HTML escaping

Why are '/' escaped?
And why function results are not?

index.html default

I'm using the template.render(ngx.var.uri ..) with a number of nested directories that have index.html in them.

Users will call app URLs such as: http://hostname/app, http://hostname/app/subdir

Is there a way to default the render to look for an index.html?
I've tried the nginx directive index index.html; but it doesn't work either.

http2 mode not working

for http2 request, it's not working.

for example:

curl --http1.1 https://example.com # works
curl https://example.com # default http2 not working

Suggestion for more friendly debugging

Sometimes I would encounter error like this:

[error] 5326#0: *1247 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/template.lua:182: invalid value (userdata) at index 7 in table for 'concat'

I wish the error log would report the exact variable name that is invalid. That would be really helpful. Thanks a lot!

Render a part of page

Hi. I have some example - I have a page. It uses dynamic structure
for example it managing some system and i can create users. So at create button i need open new some modal window/hided form/etc and write user settings. Pressing save will send ajax POST request to my server. I adding user to database and need to show new user on a page. I dont want to rewrite all page. Just a div with users table.

I generating this div but how can i put only this part of page and update i using template?
thank you.

The content within block is being attempted to be included as a file.

I have a file defining some blocks, such as

{-head-}<br>
<h1>....</h1>
<br>
<br>{-head-}

When I use template.render("some.html"), an error occurs:

20xx/xx/xx xx:xx:xx [error] 217324#217324: *10239 open() "/path/to/templates/<br>
<h1>....</h1>
<br>
<br>" failed (2: No such file or directory), other unimportant information.

I spent some time reading code, and identified the issue at

c[j+2] = '"]=include[=['

Deleting it will result in the blocks content not being processed!

I added a functionlocal function plain(v, c) return template.process_string(v, c or context) end at line 413 and replace line 584 withc[j+2] = '"]=plain[=[', and the issue disappeared.

I'm not sure this change is necessary anymore.

Directly outputting for large templates

Hi,

I have a few templates whose output is very long and I think the render() method captures all the output in a string before finally outputting it, which consumes a lot of memory.

Is there any way to get the template engine to print the output as it is generated?

Thanks,

Jason

how to set css file direcotry

how to set css file direcotry, when i use the html template?

how can i find the path css/bootstrap.min.css in html ?

How to modify rendered string?

say,local res=template.render("index.html",{})
For some reasons, before sending text to browsers, I want to do some modifications, but nginx is already rendered.
How can I just get a string without sending it to nginx?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.