handlebars-lang / handlebars-lang/handlebars.js
Handlebars does not support class instances in the template data
- Dominant language
- JavaScript
- Stars
- 18.7k
- Forks
- 2.1k
- PR merge metrics
- No merged PRs in 30d
Description
When rendering a compiled template with an array of custom class instances, [this line](https://github.com/handlebars-lang/handlebars.js/blob/f422bfdf3ed321c32c6eecfaa778de46275fb76d/lib/handlebars/runtime.js#L70) butchers the input class instance replacing it with a plain object with all custom class behaviour (getters, setters, methods etc) removed. This breaks my app.
Please remove all destructive use of `Util.extends()` on the input data - leave the input data as it is.
Some pseudocode to illustrate my case. Please let me know if you need anything else.
```js
class Car {
wheels = []
maxSpeed
colour
constructor (data) {
this.maxSpeed = data.maxSpeed
this.colour = data.colour
}
hasFourWheels () {}
}
const cars = [
new Car({ maxSpeed: 40, colour: 'silver' }),
new Car({ maxSpeed: 30, colour: 'white' })
]
handlebars.registerHelper({
customHelper: function () {
/* Fails as the input data is no longer a `Car` instance */
if (this.hasFourWheels()) {
// do something
}
}
})
/* myTemplate includes use of the `customHelper` helper. */
const compiled = handlebars.compile(myTemplate)
/* throws an error */
compiled(cars)
```
Contributor guide
Research direction
Start in lib/handlebars/runtime.js at the linked line around line 70 and trace how template data is handled during rendering. Confirm that class instances retain their custom behavior, then verify the compiled-template example no longer loses methods, getters, or setters.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- handlebars, javascript
- Domain
- web-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100