handlebars.js icon indicating copy to clipboard operation
handlebars.js copied to clipboard

Strict mode has seemingly no effect on partials

Open JemarJones opened this issue 5 years ago • 4 comments

Before filing issues, please check the following points first:

  • [x] Please don't open issues for security issues. Instead, file a report at https://www.npmjs.com/advisories/report?package=handlebars
  • [x] Have a look at https://github.com/wycats/handlebars.js/blob/master/CONTRIBUTING.md
  • [x] Read the FAQ at https://github.com/wycats/handlebars.js/blob/master/FAQ.md
  • [ ] Use the jsfiddle-template at https://jsfiddle.net/4nbwjaqz/4/ to reproduce problems or bugs (I put together an example on runkit before i realized you had a base fiddle, hopefully thats alright.

This will probably help you to get a solution faster. For bugs, it would be great to have a PR with a failing test-case.

The issue:

When using strict mode and passing a parameter into a partial, no error is thrown when that value is missing.

Example

See this running example here: https://runkit.com/jemarjones/handlebars-strict-mode-partials

const Handlebars = require("handlebars");
Handlebars.registerPartial('myPartial', 'Hello {{name}}')
const template = Handlebars.compile('{{>myPartial name=name}}', {strict: true})
console.log(template({name: 'World'})) // 'Hello World' 
// ^ Good, makes sense.

console.log(template({})) // 'Hello ' 
// ^ This should be throwing an error

const template2 = Handlebars.compile('{{name}}', {strict: true})
console.log(template2({})) // "name" not defined in [object Object] 
// ^ Not the best error message, but it does work when the missing value is used in the base template at least.

JemarJones avatar Aug 26 '20 17:08 JemarJones

Can anyone confirm whether this is as intended or whether you are open to changing this behaviour?

JemarJones avatar Sep 15 '20 18:09 JemarJones

@JemarJones I'm seeing the same behavior, and it has been throwing me off in my unit tests. I had assumed partials were beholden to strict behavior as well, but that appears to have been a bad assumption.

Thanks for the narrow example highlighting the issue.

EyePulp avatar Sep 30 '20 20:09 EyePulp

@JemarJones one more followup - I opened this issue a while ago: https://github.com/handlebars-lang/handlebars.js/issues/1681 which talks about wanting to get more detail when a partial throws an error in strict mode. What I don't remember now is why/how I was able to get those errors identified in that issue to display, because now I don't seem to see them from partials.

EyePulp avatar Sep 30 '20 20:09 EyePulp

@JemarJones one more followup - I opened this issue a while ago: #1681 which talks about wanting to get more detail when a partial throws an error in strict mode. What I don't remember now is why/how I was able to get those errors identified in that issue to display, because now I don't seem to see them from partials.

Oh interesting, it would be awesome if there is some workaround possible

JemarJones avatar Sep 30 '20 20:09 JemarJones