Is it possible to use Handlebars with objects that have numeric keys?
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?
The above link refers to accessing array items, but the answer gave me the idea to try wrapping the numeric path in square brackets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With