是否有内置的方法来循环对象的属性?
是否有 Mustache / Handlebars 方式循环访问 object 属性?
那么,
var o = {
bob : 'For sure',
roger: 'Unknown',
donkey: 'What an ass'
}
我可以在模板引擎中做一些相当于吗
for(var prop in o)
{
// with say, prop a variable in the template and value the property value
}
?
Is there a Mustache / Handlebars way of looping through an object properties?
So with
var o = {
bob : 'For sure',
roger: 'Unknown',
donkey: 'What an ass'
}
Can I then do something in the template engine that would be equivalent to
for(var prop in o)
{
// with say, prop a variable in the template and value the property value
}
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
自 Handlebars 1.0rc1 起内置支持
支持此功能 已添加到Handlebars.js,因此不再需要外部助手。
如何使用它
对于数组:
对于对象:
请注意,只有传递
hasOwnProperty
测试将被枚举。Built-in support since Handlebars 1.0rc1
Support for this functionality has been added to Handlebars.js, so there is no more need for external helpers.
How to use it
For arrays:
For objects:
Note that only properties passing the
hasOwnProperty
test will be enumerated.作为一个助手,它实际上很容易实现:
然后像这样使用它:
It's actually quite easy to implement as a helper:
Then using it like so:
编辑:车把现在有一个内置的方法来实现这一点;请参阅上面的所选答案。
使用普通 Mustache 时,以下内容仍然适用。
Mustache 可以迭代数组中的项目。因此,我建议创建一个单独的数据对象,其格式设置为 Mustache 可以使用的方式:
现在,您的 Mustache 模板将类似于:
在此处查看“非空列表”部分: https://github.com/janl/mustache.js
EDIT: Handlebars now has a built-in way of accomplishing this; see the selected answer above.
When working with plain Mustache, the below still applies.
Mustache can iterate over items in an array. So I'd suggest creating a separate data object formatted in a way Mustache can work with:
Now, your Mustache template would be something like:
Check out the "Non-Empty Lists" section here: https://github.com/janl/mustache.js
这是 @Ben 的答案,已更新以与 Ember 一起使用...请注意,您必须使用
Ember.get
因为上下文是作为字符串传入的。模板:
This is @Ben's answer updated for use with Ember...note you have to use
Ember.get
because context is passed in as a String.Template:
@Amit 的答案很好,因为它适用于 Mustache 和 Handlebars。
至于仅限车把的解决方案,我见过一些,我喜欢 https://gist.github.com/1371586 最好。
'key'
或'property'
等的对象键。@Amit's answer is good because it will work in both Mustache and Handlebars.
As far as Handlebars-only solutions, I've seen a few and I like the
each_with_key
block helper at https://gist.github.com/1371586 the best.'key'
, or'property'
, etc.代码的顺序显示特定字段
感谢 Ben 的解决方案,我的用例仅按对象
:
源对象:
模板:
输出:
Thanks for Ben's solution, my use case to display only particular fields in order
with object
Code:
Source object:
Template:
Output:
这是 MustacheJS 的辅助函数,无需预先格式化数据,而是在渲染期间获取数据。
模板:
输出:(
顺序可能是随机的 - 这是一张地图)
如果您知道所需的地图元素,这可能会很有用。只需注意虚假值即可。
This is a helper function for mustacheJS, without pre-formatting the data and instead getting it during render.
Template:
Outputs:
(order might be random - it's a map)
This might be useful if you know the map element that you want. Just watch out for falsy values.
我使用的是旧版本的车把
1.0.beta.6
,我认为在 1.1 - 1.3 期间添加了此功能,因此更新到 1.3.0 解决了该问题,以下是用法: 用法:
I was using old version
1.0.beta.6
of handlebars, i think somewhere during 1.1 - 1.3 this functionality was added, so updating to 1.3.0 solved the issue, here is the usage:Usage: