Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot use string of integer as id #99

Closed
lokkilok opened this issue Jun 27, 2011 · 10 comments
Closed

cannot use string of integer as id #99

lokkilok opened this issue Jun 27, 2011 · 10 comments
Labels

Comments

@lokkilok
Copy link

It's my first few hours working with Handlebars, so I may have missed something, but.....

With data like this: {status: {"200": 4, "304": 10}} it seems that one cannot access those entries with string representations of integers as keys. This template doesn't work for me (using the 1.0.3beta package):

{{#with status}}
{{#if 200}} OK: {{ 200 }} {{/if}}
{{/with}}

the {{ 200 }} results in a Parse error "EXPECTING ID". The same happens when the template contains: {{ "200" }}. Curiously the {{#if 200}} seems to work fine.

@wycats
Copy link
Collaborator

wycats commented Dec 27, 2011

In Handlebars, integers tokenize as INTEGER. You can make any sequence of characters work by surrounding them in brackets:

{{#with status}}
  {{#if [200] }} OK: {{ [200] }} {{/if}}
{{/with}}

@wycats wycats closed this as completed Dec 27, 2011
@santip
Copy link

santip commented Oct 26, 2012

good to know the brackets help as a workaround, but I wanted to point out that this behavior breaks backwards compatibility with mustache.

I had mustache templates where I would access array elements by literal indexes by writting:

{{someArray.0}}

However, when I tried to migrate my app from mustache to handlebars (assuming the templates should all work) I found myself with tons of errors

`Expecting 'ID', got 'INTEGER'``

Luckily, changing those expressions to

{{someArray.[0]}}

fixed the issue. perhaps it's worth it extending the language a little bit to support this experssions for mustache-compatibility.

Thanks

@wycats
Copy link
Collaborator

wycats commented Oct 27, 2012

@santip I'm confused. Mustache doesn't support paths at all, so how could you be doing {{someArray.0}} in mustache?

@santip
Copy link

santip commented Oct 27, 2012

aww, my bad, ok, I was actually using hogan.js and didn't know it wasn't supported on mustache.

btw, when migrating from hogan.js I also noticed that bubbling doesn't seem to quite work as it did in hogan (or mustache):

Handlebars.compile('aaa {{a}} {{#b}}{{c}}{{d}}{{/b}}')({a:1, b:[{c:2}], d:3}) == 'aaa 1 2'

Hogan.compile('aaa {{a}} {{#b}}{{c}}{{d}}{{/b}}').render({a:1, b:[{c:2}], d:3}) == 'aaa 1 23'

Mustache.render('aaa {{a}} {{#b}}{{c}}{{d}}{{/b}}', {a:1, b:[{c:2}], d:3}) == 'aaa 1 23'

It works when I use ../ which is the reason why I switched to Handlebars but I thought it was worth pointing out the lack of compatibility there.

@santip
Copy link

santip commented Oct 27, 2012

Also I just tested out mustache and it does seem to support paths

Mustache.render('aaa {{a}} {{b.c}}', {a:1, b:{c:2}, d:3}) == 'aaa 1 2'

@jontro
Copy link

jontro commented Oct 29, 2012

Yes, mustache supports paths. Check the # Dotted Names in https://github.com/mustache/spec/blob/master/specs/sections.yml

@wycats
Copy link
Collaborator

wycats commented Oct 31, 2012

Seems like mustache has been adding features. I am unsure if I signed up to track the Mustache spec forever :/

@wycats
Copy link
Collaborator

wycats commented Oct 31, 2012

It doesn't look like the Mustache spec describes behavior for .<integer>. I could definitely make this work, although Handlebars' foo.[anything] works better for non-identifier paths.

My original plan was "if it is a valid dot-path in JS, it works in Handlebars".

@santip
Copy link

santip commented Oct 31, 2012

Is there any reason why you couldn't support the .<integer> path notation?

I don't think you should support bubbling because it would probably break many existing handlebars templates, although you should probably update the docs and site to inform about these incompatibilities to save some headache for people doing the migration.

@georgecrawford
Copy link

I ran into this when trying to use identifiers in the form {{1}}, which compile fine in Mustache and Hogan but throw Expecting 'ID', 'DATA', got 'NUMBER' in Handlebars. Although I'd like integers to be supported, I totally understand and support @wycats approach of using only valid JS dot-path syntax.

However, I do think the docs should reflect this. They currently state:

Identifiers may be any unicode character except for the following:
Whitespace ! " # % & ' ( ) * + , . / ; < = > @ [ \ ] ^ ` { | } ~

which I think is misleading as integers aren't valid. It's much clearer to say "if it is a valid dot-path in JS, it works in Handlebars", as suggested.

http://handlebarsjs.com/expressions.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants