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

Strict mode shouldn't be enforced in conditionals #1063

Closed
nevir opened this issue Jul 23, 2015 · 3 comments
Closed

Strict mode shouldn't be enforced in conditionals #1063

nevir opened this issue Jul 23, 2015 · 3 comments
Milestone

Comments

@nevir
Copy link

nevir commented Jul 23, 2015

Currently:

let template = Handlebars.compile('{{#if foo}}{{foo}}{{/if}}', {strict: true});
template({}); // Throws `Error: "foo" not defined in [object Object]`

I definitely expected this to not be the case (you should be able to explicitly protect against missing values, IMO)!

@kpdecker
Copy link
Collaborator

kpdecker commented Aug 1, 2015

There is nothing special about if or any other helper by design. Things become very complicated if we allow decide that some subset of helpers are treated differently than others.

We could potentially not throw for missing variables for any helper variable reference, instead leaving those checks to the helper, but I'm hesitant to make such a change without hearing from users of this feature. @nzakas you requested this initially, any opinions?

@kpdecker kpdecker added this to the Next milestone Aug 1, 2015
@nzakas
Copy link

nzakas commented Aug 2, 2015

Is it the if throwing the error here? If so, I agree that's undesirable. We need some way to guard against missing data (similar to using if in JavaScript to determine if a property exists before using it).

@kpdecker
Copy link
Collaborator

kpdecker commented Aug 3, 2015

Right now the throw is done on the reference to the variable, similar to Javascript behavior when referencing an undeclared global without doing a typeof check. Just like under javascript, the if operation does not run in this case. The generated code right now is something like this:

helpers.if(strict(context, 'foo'))

Where strict is this method.

The proposed change would be for strict checking to occur for all of these references to foo, but nothing else:

  • {{foo}} : strict(context, 'foo')
  • {{foo.foo}} : strict(strict(context, 'foo'), 'foo')

But these foo references would not be checked by the generated code

  • {{bar foo bar=foo}} : helpers.bar(context.foo, {bar: context.foo})

The helper would not be able to differentiate between explicitly undefined and missing as they would just have the undefined value and not the name to do a in check.

flenter pushed a commit to flenter/handlebars.js that referenced this issue Aug 27, 2015
This allows for `{{helper foo}}` to still operate under strict mode when `foo` is not defined on the context. This allows helpers to perform whatever existence checks they please so patterns like `{{#if foo}}{{foo}}{{/if}}` can be used to protect against missing values.

Fixes handlebars-lang#1063
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