Search code examples
goluago-templatespongo2

Lua data not being rendered on pongo2 template


I'm using algernon to try to output data from a lua script into a pongo2 template.

In the code below number_list outputs exactly as I'd expect but nothing renders for object_list.

What am I doing wrong?

title = "This is the title"

number_list = {}

table.insert(number_list, 1)
table.insert(number_list, 2)
table.insert(number_list, 3)

object_list = {}

o1 = {1}
table.insert(object_list, o1)
o2 = {2}
table.insert(object_list, o2)
o3 = {3}
table.insert(object_list, o3)
<html>
  <head>
    <title>{{title}}</title>
  </head>
  <body>
    {{ title }}
    <div>

        {% for item in number_list %}
        test 1: {{item}} 
        {% endfor %}

        {% for item in object_list %}
        test 2: {{item}}
        {% endfor %}
    </div>
  </body>
</html>

edit: Turns out it's a bug. Check out @IdeaToCode answer or the bug report for a different way to do it.


Solution

  • I don't know lua but in most languages objects need to have keys. I ran this using algernon and it appears to work as you intended.

    example.lua:

    title = "This is the title"
    
    number_list = {}
    
    table.insert(number_list, 1)
    table.insert(number_list, 2)
    table.insert(number_list, 3)
    
    object_list = {}
    
    o1 = {id = 1}
    table.insert(object_list, o1)
    o2 = {id = 2}
    table.insert(object_list, o2)
    o3 = {id = 3}
    table.insert(object_list, o3)
    
    
    serve2("example.html", { object_list = object_list, number_list = number_list})
    
    

    example.html:

    <html>
    
    <head>
        <title>{{title}}</title>
    </head>
    
    <body>
        {{ title }}
        <div>
    
            {% for item in number_list %}
            test 1: {{item}}<br>
            {% endfor %}
    
            {% for item in object_list %}
            test 2: {{item.id}}<br>
            {% endfor %}
        </div>
    </body>
    
    </html>