gpt4 book ai didi

javascript - hogan js 中的复杂迭代逻辑?

转载 作者:太空宇宙 更新时间:2023-11-04 00:25:41 24 4
gpt4 key购买 nike

我正在尝试学习expressjs,我有一个查询数据库的 Controller ,并返回一个 JSON 对象,如下所示:

[ { _id: 58cb851721c1c107ab436686,                                                                                                                             
title: 'some title',
content: 'this is content' },
{ _id: 58cb856321c1c107ab436688,
title: 'ah yes',
content: 'more title and contents' }
...
...
]

我现在想要做的是在 MDL 卡布局中显示数组中的每个元素。因此,如果上面的 json 对象有 20 个对象,则应该有 20 个卡片,每个卡片显示各自的 _idtitlecontent 属性的值。为此,我必须使用类似这样的 for 循环:

for(var i = 0; i < data.length; i++) {
<span> {{ data[i]._id }} </span> </br>
<span> {{ data[i].title }} </span> </br>
<span> {{ data[i].content }} </span> </br>
}

对于像 ejs 这样的东西来说,这显然非常容易,因为它允许在模板内循环逻辑。但我不知道如何做到这一点,因为 HoganJS 缺乏适当的文档显然没有帮助。我在互联网上搜索了很多没有结果。目前我正在渲染模板,如下所示:

res.render('index');

在霍根可以做到这一点吗?如何?或者我需要在 route 做一些体操?

最佳答案

Hogan.js 是针对 mustache 测试套件开发的,因此适用于指定模板的所有内容也适用于 hogan.js。

检查https://mustache.github.io/mustache.5.html

这样的事情应该有效:

var obj = {
data: [{
_id: 58cb851721c1c107ab436686,
title: 'some title',
content: 'this is content'
}, {
_id: 58cb856321c1c107ab436688,
title: 'ah yes',
content: 'more title and contents'
},
...
]};

和模板:

{{#data}}
<span>{{_id}}</span>
<span>{{title}}</span>
<span>{{content}}</span>
{{/data}}

关于javascript - hogan js 中的复杂迭代逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42879013/

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