Lua数据在pongo2模板上没有被渲染。

huangapple go评论81阅读模式
英文:

Lua data not being rendered on pongo2 template

问题

我正在使用algernon尝试将lua脚本中的数据输出到pongo2模板中。

在下面的代码中,number_list的输出结果正如我所期望的,但object_list没有渲染任何内容。

我做错了什么?

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>

编辑:事实证明这是一个错误。查看@IdeaToCode的答案或错误报告以了解其他解决方法。

英文:

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 = &quot;This is the title&quot;

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)
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;{{title}}&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    {{ title }}
    &lt;div&gt;

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

        {% for item in object_list %}
        test 2: {{item}}
        {% endfor %}
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

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

答案1

得分: 1

我不懂Lua,但在大多数编程语言中,对象需要有键值。我使用Algernon运行了这段代码,它似乎按照你的意图工作。

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>
英文:

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 = &quot;This is the title&quot;

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(&quot;example.html&quot;, { object_list = object_list, number_list = number_list})

example.html:

&lt;html&gt;

&lt;head&gt;
    &lt;title&gt;{{title}}&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    {{ title }}
    &lt;div&gt;

        {% for item in number_list %}
        test 1: {{item}}&lt;br&gt;
        {% endfor %}

        {% for item in object_list %}
        test 2: {{item.id}}&lt;br&gt;
        {% endfor %}
    &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

huangapple
  • 本文由 发表于 2022年8月15日 11:14:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/73356591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定