sagold / handlebars-webpack-plugin

Renders your html-template at build time
161 stars 45 forks source link

Added glob support for JSON data files #41

Closed mitchheddles closed 4 years ago

mitchheddles commented 5 years ago

Adds glob support for data files. It works by producing a data object using file names as keys. This is a breaking change however... Unless we put in a check for single file results and change the data structure accordingly.

This may need some more work to support nested data folders. e.g. data/folder/file.json.

Happy to discuss.

Usage

new HandlebarsPlugin({
      entry: path.join(__dirname, 'src', 'views', '*.hbs'),
      data: path.join(__dirname, 'src', 'data', '*.json'),
})

Example

// src/data/home.json
{
  "title": "Home"
}

// src/data/about.json
{
  "title": "About"
}

// handlebars data
{
  "home": {
    "title": "Home"
  },
  "about": {
    "title": "About"
  },
}

// src/views/home.hbs
<body>
  <h1>{{home.title}}</h1>
</body>

// dist/home.html
<body>
  <h1>Home</h1>
</body>

Related: #37

kelvinlouis commented 5 years ago

Can this be merged sometime in the future?

sagold commented 4 years ago

I will not merge this PR, since merging json-files and passing this to the templates

  1. can be done outside the plugin
  2. can be done in multiple ways, that i am not trying to support

As a guideline, i added a mergeJSON-helper as reference. If it really is required to only receive a subset of the data in any given template, we could extend the onBeforeRender helper with a filepath, that is able to modify the data passed to the template.