machty / emblem.js

Emblem.js - Ember-friendly, indented syntax alternative for Handlebars.js
http://emblemjs.com
MIT License
1.04k stars 81 forks source link

Add support for block params #221

Closed Bestra closed 9 years ago

Bestra commented 9 years ago
=each collection as |item index|
   p {{item.name}}

becomes

{{#each collection as |item index|}}
  <p>{{item.name}}</p>
{{/each}}

For now I'm having the mustache parser output block params as null rather than an empty array, but that's easily changed. Comments welcome; this is the first time I've messed with a parser.

thec0keman commented 9 years ago

Would this work with components as well?

Bestra commented 9 years ago

yes. I've got this test on my local branch.

test("block form works for components", function(){
  var emblem = w(
    "= my-component as item",
    "  p {{item.name}}"
  );
  compilesTo(emblem,
    '{{#my-component as |item|}}<p>{{item.name}}</p>{{/my-component}}');
});
mixonic commented 9 years ago

Could we keep the | characters?

In general, my opinion is that mustache content should be a straight copy into htmlbars as much as possible. There are many places where that is not true today, but I consider those legacy.

When we build custom syntaxes, they make it more difficult for new users to understand what is happening. The syntaxes are less trivial to support, and need documentation.

I'd prefer to see:

=each collection as |item index|
  p {{item.name}}
thec0keman commented 9 years ago

I'm a big fan of keeping the | characters as well. This would also make it a bit more readable when you are passing in multiple parameters to a component.

Bestra commented 9 years ago

That shouldn't be a problem at all, and I agree. I think part of my reluctance to use them at first was just the roundabout way that I got the parser expressions to work. Adding the | characters will probably make the parser code simpler rather than more complex. I'll get on it shortly.

mmun commented 9 years ago

@Bestra you can make the start token as\w+| like it is in Handlebars, instead of using a separate token for as and |.

Bestra commented 9 years ago

For sure.

mixonic commented 9 years ago

@Bestra I presume you plan to back out the changes to the AST and just put the full mustache expression in the AST name?

Bestra commented 9 years ago

@mixonic those were my thoughts this morning. Then I worked 9 hours and completely forgot. Glad you're way ahead of me :smile: .

mixonic commented 9 years ago

I'm inclined to roll with this, though I do want that warm fuzzy of a +1 from @bantic.

bantic commented 9 years ago

Cool. I'll take a look this morning.

bantic commented 9 years ago

@bestra could you rename the blockParamNames plural rule to the singular? After that this is :+1: from me, thank you!

Bestra commented 9 years ago

@bantic I was lucky that the mustache parser code was really easy to understand. I went from "I will probably never be able to do this" to "holy crap I can do this" in about a half hour. Now I can finally use the new (really useful) hotness in our templates!

mixonic commented 9 years ago

Gracias @Bestra :-) Will cut a release soon.

hhff commented 9 years ago

Hi @mixonic - when do you think you'll cut a new release of this? I'm in upgrade limbo - and this is a blocker :heart: