xxgreg / mustache

Mustache template Dart library
BSD 2-Clause "Simplified" License
57 stars 59 forks source link

LambdaContext lookup should return null if no property #44

Open luaVolk opened 5 years ago

luaVolk commented 5 years ago

Let's say i have something like this

{{!index.mustache}}

{{#users}}
...
{{#isBirthday}}
HAPPY BIRTHDAY!!!
{{/isBirthday}}
...
{{/users}}
// app.dart
...

template.render({
  'users': [
    {
      'name': 'guy',
      'age': 18,
      'birthday': {'day': 15, 'month': 5},
    },
    {
      'name': 'gal',
      'age': 22,
    }
  ],
  'isBirthday': (LambdaContext ctx) {
    Map birthday = ctx.lookup('birthday'); // Causes an error because it returns an empty Object on the second person

    if (birthday != null) {
      ...
    }
  }
}, req.response)

...

Wouldn't it make more sense to return null since that's what it actually is?

xxgreg commented 5 years ago

That makes sense. I guess this is a side effect of strict mode. I guess the workaround in the meantime is to catch the exception rather that using null.

I wonder if a good backwards compatible fix would be to add a 'tryLookup' function which will return null as you require.