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

Parse error with each helper: Expecting "ID", got undefined #269

Closed
ustun opened this issue Jun 29, 2012 · 6 comments
Closed

Parse error with each helper: Expecting "ID", got undefined #269

ustun opened this issue Jun 29, 2012 · 6 comments
Labels

Comments

@ustun
Copy link

ustun commented Jun 29, 2012

I get the following error trying the example in the docs:

Uncaught Error: Parse error on line 3:
{{ #each people }} <l
-------^
Expecting 'ID', got 'undefined'

@wagenet
Copy link
Collaborator

wagenet commented Jun 29, 2012

@ustun, You'll need to show us a bit more code before we can really properly debug this. It would be most helpful if you could put together a jsfiddle for this.

@ustun
Copy link
Author

ustun commented Jun 29, 2012

@wagenet Thanks for the fast response. The problem is, I can't even demonstrate it, as it is the same as the simple #each example on the main page. If I try it in jsfiddle, or even on the Chrome console, inputting the text, it works, but for some reason, the text returned by html() fails. I'm wondering if it is somehow getting confused by newlines.

Here is the console session. As you can see, if I enter the template manually, it works.

template = $("#people-in-clients-template").html()
"

{{ #each people }}
<li>{{ this }}</li>
{{ /each }}

"
t = Handlebars.compile(template);
function (context, options) {
    if (!compiled) {
      compiled = compile();
    }
    return compiled.call(this, context, options);
  }
t({"people": ['foo','bar']})
Error: Parse error on line 3: {{ #each people }}<li> ---^ Expecting 'ID', got 'undefined'

Now trying manually:

template = "{{#each people }}<li>{{ this}}</li>{{/each}}"
"{{#each people }}<li>{{ this}}</li>{{/each}}"
t = Handlebars.compile(template);
function (context, options) {
    if (!compiled) {
      compiled = compile();
    }
    return compiled.call(this, context, options);
  }
t({"people": ['foo','bar']})
"<li>foo</li><li>bar</li>"

Trimming the html() output has no effect either.

So,

  template = $.trim($("#people-in-clients-template").html())
"

{{ #each people }}
<li>{{ this }}</li>
{{ /each }}

"
t = Handlebars.compile(template);
function (context, options) {
    if (!compiled) {
      compiled = compile();
    }
    return compiled.call(this, context, options);
  }
t({"people": ['foo','bar']})
Error: Parse error on line 3: {{ #each people }}<li> ---^ Expecting 'ID', got 'undefined'

@ustun
Copy link
Author

ustun commented Jun 29, 2012

It seems like there is some binary data in my template:

    template = "{{#each people }}<li>{{ this}}</li>{{/each}}"
"{{#each people }}<li>{{ this}}</li>{{/each}}"
/[\x00-\x1F]/.test(template)
false
template = $("#people-in-clients-template").html()
"

{{ #each people }}
<li>{{ this }}</li>
{{ /each }}

"
/[\x00-\x1F]/.test(template)
true

@ustun
Copy link
Author

ustun commented Jun 29, 2012

This is the charcodes of the problematic template:

a = [10, 10, 123, 123, 32, 35, 101, 97, 99, 104, 32, 112, 101, 111, 112, 108, 101, 32, 125, 125, 10, 123, 123, 32, 116, 104, 105, 115, 32, 125, 125, 10, 123, 123, 32, 47, 101, 97, 99, 104, 32, 125, 125, 10, 10]

b="";

for (i = 0; i < a.length; i++) {
b += String.fromCharCode(a[i]);
}

t = template({"people: ['foo','bar']});

Here is the jsfiddle:

http://jsfiddle.net/8nugY/

@ustun
Copy link
Author

ustun commented Jun 29, 2012

OK, the issue is pretty simple actually, not related to non-printing chars, but my server side templating engine put a space before }}.
The following solves the issue, though I am not sure whether handlebars should be fixed to parse {# each people }} correctly.

      source = $("#people-in-clients-template").html();
      source = source.replace(/ }}/g, "}}");
      source = source.replace(/{{ /g, "{{");

@wagenet
Copy link
Collaborator

wagenet commented Sep 15, 2012

Seems like bad encoding vs an actual bug.

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

2 participants