作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我是一名优秀的程序员,十分优秀!