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

Support for @-prefixed keywords in the each block helper #150

Merged
merged 1 commit into from
Apr 16, 2014

Conversation

lai
Copy link
Contributor

@lai lai commented Apr 15, 2014

This adds the support for data keywords prefixed by @ such as @index, @key in Handlebars' each block helper. Prior discussion about supporting @index at #112

Note that support for @first and @index in object iteration was added in Handlebars in 1.2.0. handlebars-lang/handlebars.js#661

eachIndex
= !'{' _ '@index'
eachBlockHelperVariable
= !'{' _ variable:(v:'@index' { return 'index'; } /
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I screwed up here by prematurely merging in the previous change to add support for @index; we don't want to handle @key and @index as individual special case keywords; rather, we want to support any data keyword that's prefixed with an @. This way, the helpers have full control over what data vars to add.

So this rule needs to be changed to handle @ followed by any valid identifier, not just @key and @index and whatever else might come in the future. Would you mind making this change?

@lai
Copy link
Contributor Author

lai commented Apr 15, 2014

Sounds good. Does using attributeName here seem reasonable? It's defined as

attributeName = $attributeChar*
attributeChar = alpha / [0-9] /'_' / '-'

So it would just be

eachBlockHelperVariable
  = !'{' _ '@' variableName:attributeName

@machty
Copy link
Owner

machty commented Apr 15, 2014

That seems fine to me.

@lai
Copy link
Contributor Author

lai commented Apr 15, 2014

I'm writing this test case that uses @first as the if block argument, it seems the syntax is not covered though currently:

// Support for data keywords that are prefixed with @ in the each
// block helper such as @index, @key, @first, @last
eachBlockHelperVariable
  =  _ '@' variableName:attributeName
{
  var params = {};
  var idNode = new AST.IdNode([{part: variableName}]);
  var dataNode = new AST.DataNode(idNode);
  params = dataNode;
  return new AST.MustacheNode([params], null, false);
}


recursivelyParsedMustacheContent
  = !'{' text:$[^}]*
{
  // Force interpretation as mustache.
  // TODO: change to just parse with a specific rule?
  text = "=" + text;
  return Emblem.parse(text).statements[0];
}

mustacheContent
 = eachBlockHelperVariable / recursivelyParsedMustacheContent

Is there a better place to define the eachBlockHelperVariable rather than just as some kind of mustacheContent?

@machty
Copy link
Owner

machty commented Apr 15, 2014

Hmm, honestly, this commit should be reverted: f15ffef

There's no reason the parsing of @data keyword should be a sibling of recursive mustache content. It should be parsed like any other mustache-y identifier.

Sorry you've been on this wild goose chase; if you still have energy, and I hope you do, here's what you should do:

  1. Revert the above commit
  2. Add rules somewhere closer to pathIdNode; pathIdNode is used in a few different places, both as a line-starting mustache, an argument to helpers, and a value for attributes.
  3. Keep the test cases you already have as well as the one from the above reverted commit, and add additional ones for the cases described in Disable 'view' capitalization helper if suffix modifiers are in place #2.

The recursiveMustacheParse thing is used in the case that you have something like

p Some text and a #{rescursivelyParsedMustache}

It basically restarts the Emblem parser on everything between the curly braces, but you shouldn't have to touch that in any way to get data vars to work.

It shouldn't be an insane amount of work, but you're correct that support should also exist for using data keywords as helper args, in addition to making the logic universal beyond just @index

@lai
Copy link
Contributor Author

lai commented Apr 15, 2014

Tests all passed now for Handlebars 1.3.0 and 1.2.1. Since Handlebars 1.1.2 doesn't have support for these iteration variables, is there any way we can conditionally skip some of the tests?

@machty
Copy link
Owner

machty commented Apr 15, 2014

@lai Awesome; yeah, i suppose you can check Handlebars.VERSION to decide whether or not to run that test.

@lai lai changed the title @key support for object iteration in each block Support for @ prefixed keywords in each helper block Apr 16, 2014
@lai
Copy link
Contributor Author

lai commented Apr 16, 2014

@machty Please review the amended commit.

@lai lai changed the title Support for @ prefixed keywords in each helper block Support for @-prefixed keywords in each helper block Apr 16, 2014
@lai lai changed the title Support for @-prefixed keywords in each helper block Support for @-prefixed keywords in the each block helper Apr 16, 2014
machty added a commit that referenced this pull request Apr 16, 2014
Support for @-prefixed keywords in the each block helper
@machty machty merged commit 1df3849 into machty:master Apr 16, 2014
@machty
Copy link
Owner

machty commented Apr 16, 2014

Looks good, thank you!

@lai
Copy link
Contributor Author

lai commented Apr 16, 2014

Thanks! Can't wait to fully embrace Emblem here :)

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

Successfully merging this pull request may close these issues.

2 participants