valotas / mustache4dart

mustache implementation for Dart
Other
88 stars 15 forks source link

lambdas now take 2 params, the second being the local context #40

Closed sethladd closed 9 years ago

sethladd commented 9 years ago

Addresses https://github.com/valotas/mustache4dart/issues/39

All tests are passing on my machine.

NOTE: this is a breaking change. Lambdas now expect two params: the string and the context. Consider bumping version to 1.1.

If you want to support one or two parameter lambdas, you can do it.

typedef OneParamLambda(String s);
typedef TwoParamLambda(String s, dynamic ctx);

And then in your lambda calling logic:

if (lambda is OneParamLambda) {
  return lambda(string);
} else if (lambda is TwoParamLambda) {
  return lambda(string, ctx);
}

I did not do this, as I'm not sure if you care.

BTW is context always a MustacheContext ? I wasn't sure, so I left it dynamic.

valotas commented 9 years ago

I believe this is against the lambda specs. Lambdas' arity is either zero or one.

Do you have a real world example for that? Have in mind that as you are in charge of the given lambda, you can make use of whatever context you feel like within it. Why would you like the library to provide you with one?

sethladd commented 9 years ago

For use case, see https://github.com/valotas/mustache4dart/issues/39

Because my lambda is in a loop, I have no idea which item I'm currently looping over. I need that context passed to me.

valotas commented 9 years ago

No need for merge as the issue has been fixed with 0b2e12665f071636baea2ed14fc113f97f7f0769. It still uses most of your code. Thanks

sethladd commented 9 years ago

No problem. Thanks!