sagold / handlebars-webpack-plugin

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

Use with multiple json files? #37

Open JohnBlazek opened 6 years ago

JohnBlazek commented 6 years ago

I'd like to use more than one json file as a data source in my project. Currently, It seems I can only use 1. Is this possible?

I've tried globbing the data:

            entry: path.join(process.cwd(), 'tmp/*.hbs'),
            output: path.join(process.cwd(), 'target/www/[name].html'),
            data: path.join(process.cwd(), 'src/data/*.json'),
            partials: [
                path.join(process.cwd(), 'src/**/*.hbs')
            ]
        }),

and tried adding data as an array:

            entry: path.join(process.cwd(), 'tmp/*.hbs'),
            output: path.join(process.cwd(), 'target/www/[name].html'),
            data: [path.join(process.cwd(), 'src/data/file1.json'), path.join(process.cwd(), 'src/data/file2.json')],
            partials: [
                path.join(process.cwd(), 'src/**/*.hbs')
            ]
        }),

Neither is returning what i would expect. Globbing returns an error:

Tried to read /DIR/src/**/*.json as json-file and failed. Using it as data source...

Adding data as array returns no data in my template.

Any help here is appreciated!

jw-miaem commented 6 years ago

Also wondering if this is possible? was hoping to replicate similiar functionality to mustache starter kit. I have kind of replicated the behaviour using mutliple instances of the plugin but the partials get rendered multiple times. Ideally just want to run one instance with multiple json files for different templates and pages

sagold commented 5 years ago

Hi.

I did not want this feature being a part of the plugin, since the json input by file or object is a clean interface for the data. Anything like merging, downloading or other stuff could easily be done before the webpack compilation. Now, since a PR is proposed, i will consider maintaining this feature.

sagold commented 4 years ago

As of now, you can follow the example to merge json-files or use the helper function within this repository: https://github.com/sagold/handlebars-webpack-plugin#merging-input-data