gpt4 book ai didi

javascript - Hogan JS 模板中的 For 循环

转载 作者:行者123 更新时间:2023-11-29 10:12:48 26 4
gpt4 key购买 nike

我正在使用 Express JS 和 Hogan JS 模板引擎。我知道 hogan 是无逻辑模板,但我需要在 View 代码中执行 for 循环 以生成表字段。

我已经做了很多谷歌搜索,但没有找到任何解决方案。我知道如何在 Hogan JS 中执行 if-else

我阅读了 Hogan JS 中的所有文档和 Mustache JS网站。

我正在获取 json 格式的值。

[
{
"email": "abc@example.com",
"name": "abc",
"date": "05/01/2015"
},
{
"email": "xyz@example.com",
"name": "xyz",
"date": "05/01/2015"
}
]

这是示例json,可能有任意数量的数据。要在 View 中的表中显示此数据,我需要迭代一个循环。所以我需要一个for循环代码。

最佳答案

你当然可以做到。

将数据分配到嵌套的 JSON 对象中,并为父键编译模板。

var data = {"list" : [
{
"email": "abc@example.com",
"name": "abc",
"date": "05/01/2015"
},
{
"email": "xyz@example.com",
"name": "xyz",
"date": "05/01/2015"
}
]};
var template = Hogan.compile("{{#list}} Your name is {{name}} and email is {{email}} <br/>{{/list}}");
var output = template.render(data);

这是工作 example

关于javascript - Hogan JS 模板中的 For 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29998595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com