handlebars-webpack-plugin
handlebars-webpack-plugin copied to clipboard
Data
I've got this in my config:
// data passed to main hbs template: `main-template(data)`
data: require("./public/build/manifest.json"),
I can't seem to access the data in an .hbs file. I'd prefer to get at the data in a helper. I am looking to parse that json in a helper and write some script tags.
Do you have an example using data:
?
Wondering also if I were to use something like
onBeforeSetup: function (Handlebars) {
title: 'test'
},
It does not seem to be available like {{HandlebarsPlugin.title}}
Is there a way to do that?
Hi daveferrara.
The data is simply passed to the template function as explained in the handlebars docs. To access the data within a helper you have two options:
- Pass the data to a helper directly (recommended):
{{myhelper dataInScope}}
or - Bind the data via scope to your helper
To bind the data in webpack.config
const data = require("./public/build/manifest.json");
// ...
plugins: [
new HandlebarsPlugin({
data,
helpers: {
myhelper: function () {
// access data
}
}
})
Thank you for the answer 👍
@sagold Thanks.