Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using objects with numeric properties in handlebars.js

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.

like image 731
freethejazz Avatar asked Jan 16 '13 18:01

freethejazz


1 Answers

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.

like image 62
freethejazz Avatar answered Nov 18 '22 17:11

freethejazz