Skip to content

Instantly share code, notes, and snippets.

@twolfson
Created September 16, 2012 05:29
Show Gist options
  • Save twolfson/3731124 to your computer and use it in GitHub Desktop.
Save twolfson/3731124 to your computer and use it in GitHub Desktop.
Grunt mustache re-implementation
// At this time, grunt-mustache was giving me shit so I rewrote it for what I needed...
module.exports = function (grunt) {
// Project configuration.
grunt.initConfig({
mustache: {
template: {
src: 'template.svg.mustache',
dest: 'template.svg',
partials: 'partials/*.mustache'
}
},
watch: {
mustache: {
files: '<config:mustache.template.src>',
tasks: 'mustache'
}
}
});
// Load local tasks.
var mustache = require('mustache'),
fs = require('fs'),
path = require('path');
grunt.registerMultiTask('mustache', 'Render mustache files', function () {
var data = this.data,
srcFile = data.src,
srcContent = grunt.file.read(srcFile),
partials = data.partials || [],
partialArr = grunt.file.expandFiles(partials),
partialMap = {};
partialArr.forEach(function (partial) {
var partialItem = grunt.file.read(partial),
filename = path.basename(partial, '.mustache');
partialMap[filename] = partialItem;
});
var retVal = mustache.render(srcContent, {}, partialMap),
destFile = data.dest;
grunt.file.write(destFile, retVal);
});
// Default task.
grunt.registerTask('default', 'mustache');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment