英文:
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 = "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.
答案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 = "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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论