handlebars-webpack-plugin icon indicating copy to clipboard operation
handlebars-webpack-plugin copied to clipboard

Data

Open daveferrara1 opened this issue 7 years ago • 4 comments

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: ?

daveferrara1 avatar Feb 22 '17 17:02 daveferrara1

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?

daveferrara1 avatar Feb 22 '17 18:02 daveferrara1

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:

  1. Pass the data to a helper directly (recommended): {{myhelper dataInScope}} or
  2. 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
                }
            }
        })

sagold avatar Feb 28 '17 09:02 sagold

Thank you for the answer 👍

Pieter-Rees avatar Feb 28 '17 11:02 Pieter-Rees

@sagold Thanks.

daveferrara1 avatar Feb 28 '17 17:02 daveferrara1