Unfortunately, the gulp-compile-handlerbars takes two arguments , the first of which is all the data passed into your templates. This means that you need to download all your json files and transfer them as a single object.
You can do this with a little helper, for example:
function requireJsons(fileNames) { return fileNames.reduce(function(jsons, fileName) { jsons[fileName] = require('app/content/' + fileNames[i] + '.json'); return jsons; }, {}); }
What you can use to create a data object for all your templates:
var data = requireJsons(['intro', 'header', 'faq', 'features', 'footer']); gulp.task('html', function() { return gulp.src('./app/templates/*.hbs') .pipe(handlebars(data, {
If you always need to download all json files from the app/content directory, you can use readdirSync to get all .json file names and then pass them to requireJsons :
var path = require('path'); var fileNames = fs.readdirSync('app/content') .filter(function(fileName) { return path.extname(fileName) === '.json'; }); var data = requireJsons(fileNames);
Of course, if speed is important, you can combine the two methods into one method, which loads jsons and builds a data object in one pass.
Another option is the ability to individually compile each template and transfer the corresponding data to each compilation. It would be useful to use the gulp-foreach tool.
source share