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

Use with multiple json files?

Open JohnBlazek opened this issue 6 years ago • 3 comments

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!

JohnBlazek avatar Jul 26 '18 13:07 JohnBlazek

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

jw-miaem avatar Sep 02 '18 08:09 jw-miaem

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 avatar Sep 15 '18 09:09 sagold

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

sagold avatar Nov 23 '19 11:11 sagold