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

Add support for nesting stache helpers #1769

Closed
dylanrtt opened this issue Jul 2, 2015 · 6 comments
Closed

Add support for nesting stache helpers #1769

dylanrtt opened this issue Jul 2, 2015 · 6 comments

Comments

@dylanrtt
Copy link
Contributor

dylanrtt commented Jul 2, 2015

Handlebars supports putting the result of one stache helper in another. They call it Subexpressions.

{{outer-helper (inner-helper 'abc') 'def'}}

Angular also allows chaining filters but there is currently no way to do this type of thing in the CanJS implementation of mustache/stache.

@asavoy
Copy link
Contributor

asavoy commented Jul 3, 2015

+1 I'd like this.

@justinbmeyer
Copy link
Contributor

@dylanrtt @asavoy any interest in helping implement it? I can walk through what's needed.

Anyone think that:

{{outer-helper inner-helper('abc') 'def'}}

Would be more natural?

@justinbmeyer
Copy link
Contributor

The place to start implementing this is to first get the parsing of sub-expressions working. Currently, the parsing happens here:

https://github.com/bitovi/canjs/blob/master/view/stache/mustache_core.js#L133

For something like {{outer-helper 1 name zed='ted' foo=bar}} this returns a data structure that looks like:

{
  name: "outer-helper",
  args: [1, {get: 'name'}],
  hashes: {zed: 'ted', foo: {get: 'bar'}}
}

To make sub-expressions work, we'd want to make this recursive. Something like {{outer-helper inner-helper('abc') 'def'}} might return:

{
  name: "outer-helper",
  args: [{name: 'inner-helper', args: ['def']}],
  hashes: {}
}

With that in place, the next work would need to be done in makeEvaluator: https://github.com/bitovi/canjs/blob/master/view/stache/mustache_core.js#L182

There is looks in args and in hashes for objects "getter" objects:

https://github.com/bitovi/canjs/blob/master/view/stache/mustache_core.js#L208

If it found an object like {name, args, hashes}, it could actually call itself (makeEvaluator) with that object as exprData. makeEvaluator returns a function that will need to be converted into a compute.

All in all .. pretty simple actually.

@justinbmeyer
Copy link
Contributor

Related to #1888, I'm thinking that function calls are more natural when you might have multiple of them. Compare:

{{#each ( (game.statsForPlayerId player.id).filter type) }}

vs

{{#each gameStatsForPlayerId(player.id).filter(type) }}

@dylanrtt
Copy link
Contributor Author

dylanrtt commented Sep 8, 2015

I like the function call method filter(type) better than the way handlebars does it.

@dylanrtt
Copy link
Contributor Author

dylanrtt commented Nov 4, 2015

This works now, with helpers and functions. I think it was added via #1832.

Working example with 2.3.1:
http://jsbin.com/dakesokuva/edit?html,js,output

@dylanrtt dylanrtt closed this as completed Nov 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants