简体   繁体   English

在handlebars.js中使用具有数字属性的对象

[英]Using objects with numeric properties in handlebars.js

Is it possible to use Handlebars with objects that have numeric keys? 是否可以将Handlebars与具有数字键的对象一起使用?

For example: 例如:

var str = "<div>{{apples}}</div>",
    tmpl = Handlebars.compile(str);

tmpl({apples: "works!"}); // returns "<div>works!</div>"

Works great, but 效果很好,但是

var str = "<div>{{4}}</div>",
    tmpl = Handlebars.compile(str);

tmpl({4: "works!"}); 
// returns Error: Parse error on line 1: <div>{{4}}</div> -------^ Expecting 'DATA', 'ID', got 'INTEGER'

I was unable to find any references to this when searching, and I have not yet explored the source code. 我在搜索时找不到任何对此的引用,我还没有探索过源代码。

So after looking around further, I found I needed to wrap the number in square brackets, like: 所以在进一步观察之后,我发现我需要将数字包装在方括号中,例如:

{{[4]}}

Here's a link to a semi-relevant SO question How do I access an access array item by index in handlebars? 这是一个半相关SO问题的链接如何通过把手中的索引访问访问数组项目?

The above link refers to accessing array items, but the answer gave me the idea to try wrapping the numeric path in square brackets. 上面的链接指的是访问数组项,但答案让我想到尝试将数字路径包装在方括号中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM