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

There should be a configuration option to throw if a tag finds no match in the source #651

Closed
christothes opened this issue Nov 10, 2013 · 7 comments · Fixed by #932
Closed
Labels
Milestone

Comments

@christothes
Copy link

Given data with a missing property represented in the source, it should be configurable to have this throw.

var source = 'Some text {{foo}} with {{missing}} tag';
var template = Handlebars.compile(source);
var data = { foo : 'bar'};

//should optionally throw
var result = template(data); 

@showell
Copy link

showell commented Dec 4, 2013

I agree with this request. My workaround for now is that I have a utility that can extract the required data structure from a template, which I can then use to validate the data, rather than making it the job of the compiler. See the gist here: https://gist.github.com/showell/7793101

@rgould
Copy link

rgould commented Feb 14, 2014

👍 (I would say that this should be the default)

@abritinthebay
Copy link

I'd say the default should be a warning, maybe with console.warn if available, not an error; but the concept is great.

@Baccanno
Copy link

Baccanno commented Jul 1, 2014

Mustache already has a test mechanism {{#here}} my stuff is {{here}} {{/here}}
{{^here}} my stuff is not here {{/here}}

Plus it does not match mustach specs. Why not an option, certainly not a warning or a default behaviour IMO.

@abritinthebay
Copy link

One idea could be a simple tester:

var source = 'Some text {{foo}} with {{missing}} tag';
var template = Handlebars.compile(source);
var data = { foo : 'bar'};

if (Handlebars.hasAllProperties(template, data)) {
    var result = template(data); 
}

That way it's not core to the rest of the Handlebars code, allows the ability to test schema's against an actual template, and keeps current behavior.

Only issue I can see is that the compiler may need to be changed to allow collation of the variables.

@kpdecker kpdecker modified the milestones: 2.1, Next Sep 4, 2014
kpdecker added a commit that referenced this issue Dec 27, 2014
Causes templates to throw when lookup fields are not defined within the context. Strict mode will throw when any field is omitted. assumeObjects mode maintains the existing behavior of outputting an empty response when fields are not defined but allows for performance/size optimizations by not doing safety checks for intermediate objects in the lookup chain.

Strict mode effectively disables the helperMissing and inverse handling for blockHelperMissing as templates will throw rather than call those features for missing data fields.

Fixes #651
Fixes #805
@kpdecker
Copy link
Collaborator

Closing in favor of #932

@ajs139
Copy link

ajs139 commented Mar 28, 2019

Converted @showell's gist to TypeScript, in case it helps anyone: https://gist.github.com/ajs139/f84ccbe5678ef1497a53965cc326b452

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants