handlebars-lang / handlebars.js

Minimal templating on steroids.
http://handlebarsjs.com
MIT License
17.95k stars 2.04k forks source link

Request: "strict" lookups #805

Closed nzakas closed 9 years ago

nzakas commented 10 years ago

One thing that has bitten me in Handlebars multiple times is referencing a property that doesn't exist, such as:

<p>{{foo.bar}}</p>

If foo.bar doesn't exist, then I get no indication in the output that it's missing. I understand that as a default behavior, but I'd prefer to throw an error in that case.

I know I can implement this myself by inheriting from JavaScriptCompiler, I was just curious if this would be considered as an available option built-in to Handlebars?

kpdecker commented 10 years ago

This is something that we'd consider as an option but as with every additional option flag I'd like to see the impact on both the library size and the size of the generated code, if a given feature requires a lot of overhead in either situation then it might not make the cut.

That being said, once we implement source map support, this can actually make the lookup much safer as the check for undefined on foo would not be necessary.

kpdecker commented 10 years ago

This is closely related to #651

nzakas commented 10 years ago

I haven't had time to keep investigating, but my original thought was to:

  1. Create a helper that does the strict lookup.
  2. Override the mustache() method on the compiler to rewrite values to use that helper.

I was modeling the approach after the SproutCore extensions: https://gist.github.com/SlexAxton/1187974#file-02-schandlebars-js-L54

I didn't get very far down that path before other priorities bubbled up, but at least on the surface, it seemed like the amount of code changes to make this work would be small.

kpdecker commented 9 years ago

Closing in favor of #932