Last active
December 21, 2015 13:09
-
-
Save hail2u/6310613 to your computer and use it in GitHub Desktop.
This Grunt task renders `foo.hbs` using data based on `metadata.json` (Base) and `foo.json` (Extend) with Handlebars.js. `foo.hbs` can include other Handlebars.js template (e.g. `styles.inc.hbs`).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (grunt) { | |
var _ = grunt.util._; | |
var async = grunt.util.async; | |
var hbs = require('handlebars'); | |
var path = require('path'); | |
var taskName = 'generate'; | |
var taskDescription = 'Generate files using Handlebars.js.'; | |
grunt.registerMultiTask(taskName, taskDescription, function () { | |
var done = this.async(); | |
var dirTmpl = this.data.cwd; | |
var metadataBase = grunt.file.readJSON(dirTmpl + 'metadata.json'); | |
hbs.registerHelper('include', function (file, options) { | |
var rendered = _applyTemplate(dirTmpl + file, this); | |
return new hbs.SafeString(rendered); | |
}); | |
async.forEach(this.files, function (file, next) { | |
var template = file.src[0]; | |
if (!grunt.file.exists(template)) { | |
grunt.log.warn('Source file "' + template + '" not found.'); | |
next(); | |
} | |
dirTmpl = path.dirname(template) + '/'; | |
var metadata = _extendData(template); | |
var rendered = _applyTemplate(template, metadata); | |
grunt.file.write(file.dest, rendered); | |
grunt.log.writeln('File "' + file.dest + '" created.'); | |
next(); | |
}, function (err) { | |
done(err); | |
}); | |
function _applyTemplate(file, data) { | |
var tmpl = hbs.compile(grunt.file.read(file)); | |
return tmpl(data); | |
} | |
function _extendData(file) { | |
var data = _.extend({}, metadataBase); | |
var fileMetadata = file.replace(/\.\w+?$/, '.json'); | |
if (grunt.file.isFile(fileMetadata)) { | |
_.extend(data, grunt.file.readJSON(fileMetadata)); | |
} | |
return data; | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (grunt) { | |
grunt.initConfig({ | |
generate: { | |
all: { | |
expand: true, | |
cwd: '.grunt/html/', | |
src: ['**/*.hbs', '!**/*.inc.hbs'], | |
filter: 'isFile', | |
dest: './', | |
ext: '.html' | |
} | |
} | |
}); | |
grunt.util.linefeed = '\n'; | |
grunt.loadTasks('.grunt/tasks/'); | |
grunt.registerTask('default', []); | |
grunt.registerTask('rebuild', [ | |
'generate:all', | |
]); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"canonical": "http://example.com/" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"title": "My Awesome Website", | |
"description": "I love internet!" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[.grunt] | |
[html] | |
metadata.json | |
index.hbs | |
index.json | |
styles.inc.hbs | |
[sub] | |
subdir.hbs | |
subdir.json | |
[tasks] | |
generate.js | |
Gruntfile.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"canonical": "http://example.com/subdir/", | |
"title": "My Awesome Sub-directory!", | |
"description": "I love internet, too!" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment