把手/胡须 - 是否有内置的方式来循环对象的属性? [英] Handlebars/Mustache - Is there a built in way to loop through the properties of an object?

查看:124
本文介绍了把手/胡须 - 是否有内置的方式来循环对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题的标题所说,是否有一种循环对象属性的小胡子/句柄方式?



p>

  var o = {
bob:'肯定',
roger:'未知',
驴子:'什么屁股'
}

然后我可以做些什么在模板引擎中相当于

  for(var prop in o)
{
//用say说明,在模板中设置一个变量并赋值属性值
}



p>支持此功能已添加 到Handlebars.js,因此不再需要外部帮助。



如何使用它



对于数组:

  {{#each myArray}} 
索引:{{ index}} Value = {{this}}
{{/ each}}

对象:

  {{#each myObject}} 
键:{{@key}} Value = {{this }}
{{/ each}}

请注意,只有传递 hasOwnProperty 测试将被枚举。


As the title of question says, 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
}

?

解决方案

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:

{{#each myArray}}
    Index: {{@index}} Value = {{this}}
{{/each}}

For objects:

{{#each myObject}}
    Key: {{@key}} Value = {{this}}
{{/each}}

Note that only properties passing the hasOwnProperty test will be enumerated.

这篇关于把手/胡须 - 是否有内置的方式来循环对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆