diff --git a/Gruntfile.js b/Gruntfile.js index b621002..1338a23 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -62,6 +62,13 @@ module.exports = function(grunt) { templateData: 'test/fixtures/data.json', output: 'tmp/dynamicPost.html' }, + anyArray: { + template: ['test/fixtures/deep/romanian.handlebars', 'test/fixtures/deep/german.handlebars'], + templateData: ['test/fixtures/deep/romanian.json', 'test/fixtures/deep/german.json'], + output: ['tmp/deep/romanian.html','tmp/deep/german.html'], + helpers: ['test/helpers/super_helper.js'], + partials: ['test/fixtures/deep/shared/foo.handlebars'] + }, globbedTemplateAndOutput: { template: 'test/fixtures/deep/**/*.handlebars', templateData: 'test/fixtures/deep/**/*.json', diff --git a/README.md b/README.md index fa06192..fa35b02 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,13 @@ Heres a few of the ways you can use it templateData: 'test/fixtures/data.json', output: 'tmp/dynamicPost.html' }, + allArray: { + template: ['test/fixtures/deep/spanish.handlebars', 'test/fixtures/deep/deeper/portuguese.handlebars'], + templateData: ['test/fixtures/deep/spanish.handlebars', 'test/fixtures/deep/deeper/portuguese.json'], + output: ['tmp/deep/spanish.html','tmp/deep/deeper/portuguese.html'], + helpers: ['test/helpers/super_helper.js'], + partials: ['test/fixtures/deep/shared/foo.handlebars'] + }, globbedTemplateAndOutput: { template: 'test/fixtures/deep/**/*.handlebars', templateData: 'test/fixtures/deep/**/*.json', @@ -82,7 +89,7 @@ Heres a few of the ways you can use it 'test/globals/info.json', 'test/globals/textspec.json' ] - } + } } ``` diff --git a/tasks/compile-handlebars.js b/tasks/compile-handlebars.js index 51eb8b6..61913e2 100644 --- a/tasks/compile-handlebars.js +++ b/tasks/compile-handlebars.js @@ -61,6 +61,7 @@ module.exports = function(grunt) { * and if so, returns the unglobbed * version of the filename */ var isGlob = function(filename) { + if (!filename) return; var match = filename.match(/[^\*]*/); if (match[0] !== filename) { return match.pop(); @@ -71,8 +72,9 @@ module.exports = function(grunt) { * any globs are used, so the globbed outputs * can be generated */ var getBasename = function(filename, template) { - var glob = isGlob(template); - var basename; + var basename, glob; + template = Array.isArray(template) ? filename : template; + glob = isGlob(template); if (glob) { basename = filename.slice(glob.length, filename.length).split('.'); basename.pop(); @@ -84,7 +86,13 @@ module.exports = function(grunt) { return basename.join('.'); }; - var getName = function(filename, basename) { + var getName = function(filename, basename, index) { + if (Array.isArray(filename)) { + var file = filename[index]; + if (file) return file; + grunt.log.error('You need to assign the same number of ouputs as you do templates when using array notation.'); + return; + } if (typeof filename === 'object') { return filename; } @@ -138,29 +146,29 @@ module.exports = function(grunt) { handlebars.registerPartial(basename, require(fs.realpathSync(partial))); }); - templates.forEach(function(template) { + templates.forEach(function(template, index) { var compiledTemplate = handlebars.compile(parseData(template)); var basename = getBasename(template, config.template); var html = ''; var json; if (config.preHTML) { - html += parseData(getName(config.preHTML, basename)); + html += parseData(getName(config.preHTML, basename, index)); } if (config.globals) { - json = mergeJson(parseData(getName(templateData, basename)), config.globals); + json = mergeJson(parseData(getName(templateData, basename, index)), config.globals); } else { - json = parseData(getName(templateData, basename)); + json = parseData(getName(templateData, basename, index)); } html += compiledTemplate(json); if (config.postHTML) { - html += parseData(getName(config.postHTML, basename)); + html += parseData(getName(config.postHTML, basename, index)); } - grunt.file.write(getName(config.output, basename), html); + grunt.file.write(getName(config.output, basename, index), html); }); process.nextTick(done); diff --git a/test/compile_handlebars_test.js b/test/compile_handlebars_test.js index e836a44..4cc53f2 100644 --- a/test/compile_handlebars_test.js +++ b/test/compile_handlebars_test.js @@ -53,6 +53,18 @@ exports.clean = { test.done(); }, + allArray: function(test) { + test.expect(2); + var germanActual = grunt.file.read('tmp/deep/german.html'); + var romanianActual = grunt.file.read('tmp/deep/romanian.html'); + var germanExpected = grunt.file.read('test/expected/german.html'); + var romanianExpected = grunt.file.read('test/expected/romanian.html'); + + test.equal(germanActual, germanExpected); + test.equal(romanianActual, romanianExpected, 'array output should be working'); + + test.done(); + }, globbedTemplateAndOutput: function(test) { test.expect(2); diff --git a/test/expected/german.html b/test/expected/german.html new file mode 100644 index 0000000..774a419 --- /dev/null +++ b/test/expected/german.html @@ -0,0 +1 @@ +