Compile handlebar templates, outputting static HTML
npm install grunt-compile-handlebarsCompiles handlebar templates, outputs static HTML
npm install grunt-compile-handlebarsThen add this line to your project's Gruntfile.js gruntfile:
``javascript`
grunt.loadNpmTasks('grunt-compile-handlebars');
[grunt]: https://github.com/gruntjs/grunt
[getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md
`javascript`
'compile-handlebars': {
allStatic: {
files: [{
src: 'test/fixtures/template.handlebars',
dest: 'tmp/allStatic.html'
}],
preHTML: 'test/fixtures/pre-dev.html',
postHTML: 'test/fixtures/post-dev.html',
templateData: 'test/fixtures/data.json'
},
dynamicHandlebars: {
files: [{
src: '',
dest: 'tmp/dynamicHandlebars.html'
}],
templateData: {},
handlebars: 'node_modules/handlebars'
},
jsonHandlebars: {
files: [{
src: 'test/fixtures/sweedishTemplate.json',
dest: 'tmp/sweedish.json'
}],
templateData: 'test/fixtures/sweedishData.json'
},
dynamicTemplate: {
files: [{
src: '{{salutation}}{{punctuation}} {{location}}
',
dest: 'tmp/dynamicTemplate.html'
}],
template: '{{salutation}}{{punctuation}} {{location}}
',
templateData: 'test/fixtures/data.json'
},
dynamicTemplateData: {
files: [{
src: 'test/fixtures/template.handlebars',
dest: 'tmp/dynamicTemplateData.html'
}],
templateData: {
"salutation": "Hallo",
"punctuation": ",",
"location": "Welt"
}
},
dynamicPre: {
files: [{
src: 'test/fixtures/template.handlebars',
dest: 'tmp/dynamicPre.html'
}],
preHTML: '
templateData: 'test/fixtures/data.json'
},
dynamicPost: {
files: [{
src: 'test/fixtures/template.handlebars',
dest: 'tmp/dynamicPost.html'
}],
postHTML: '',
templateData: 'test/fixtures/data.json'
},
anyArray: {
files: [{
src: ['test/fixtures/deep/romanian.handlebars', 'test/fixtures/deep/german.handlebars'],
dest: ['tmp/deep/romanian.html','tmp/deep/german.html']
}],
templateData: ['test/fixtures/deep/romanian.json', 'test/fixtures/deep/german.json'],
helpers: ['test/helpers/super_helper.js'],
partials: ['test/fixtures/deep/shared/foo.handlebars']
},
globbedTemplateAndOutput: {
files: [{
expand: true,
cwd: 'test/fixtures/',
src: 'deep/*/.handlebars',
dest: 'tmp/',
ext: '.html'
}],
templateData: 'test/fixtures/deep/*/.json',
helpers: 'test/helpers/*/.js',
partials: 'test/fixtures/deep/shared/*/.handlebars'
},
globalJsonGlobbedTemplate: {
files: [{
expand: true,
cwd: 'test/fixtures/',
src: 'deep/*/.handlebars',
dest: 'tmp/',
ext: '.html'
}],
templateData: 'test/fixtures/deep/*/.json',
helpers: 'test/helpers/*/.js',
partials: 'test/fixtures/deep/shared/*/.handlebars',
globals: [
'test/globals/info.json',
'test/globals/textspec.json',
{
textspec: {
"ps": "P.S. from Gruntfile.js"
}
}
]
},
registerFullPath: {
files: [{
src: '{{salutation}}{{punctuation}} {{location}}
{{> test/fixtures/deep/shared/pathTest}}',
dest: 'tmp/fullPath.html'
}],
templateData: {
"salutation": "Hallo",
"punctuation": ",",
"location": "Welt"
},
partials: 'test/fixtures/deep/shared/*/.handlebars',
registerFullPath: true
},
concatGlobbed: {
files: [{
src: 'test/fixtures/deep/*/.handlebars',
dest: 'tmp/concatGlobbed.html'
}],
templateData: 'test/fixtures/deep/*/.json'
},
oneTemplateToManyOutputs: {
files: [{
src: 'test/fixtures/template.handlebars',
dest: ['tmp/oneTemplateToManyOutputs1.html', 'tmp/oneTemplateToManyOutputs2.html']
}],
templateData: ['test/fixtures/oneTemplateToManyOutputs1.json', 'test/fixtures/oneTemplateToManyOutputs2.json']
}
},
The available configuration options are as follows
Unless otherwise noted, all configurable values can be represented as
* a string representing the path to a specific file
* a string representing the path to a globbed representation of the files, matched up against the values resolved from the template configuration
* an array of literal paths, globbed paths, or a combination of the two
__files__ - A typical grunt files object. The src are your handlebar templates, the dest is your html ouput. See the grunt documentation and the usage examples above for more info on how to use this object.
__preHTML__ - Static text to be inserted before the compiled templatepostHTML
____ - Static text to be inserted after the compiled template
__templateData ~~ The data being fed to compiled template, in addition to the normal configurable values, this can be
* an inline string representation of a data (I don't know why you would do that though, when you can do...)
* an inline JSON representation of a data
__globals__ - globals that can be included, useful for when you have template specific data, but want some data available to all templateshelpers
____ - handlebars helperspartials
____ - handlebars partials
__registerFullPath__ - normally, helpers and partials are registered under their basename, rather than their path (e.g. partial at partials/deep/awesomePartial.handlebars is registered as {{> awesomePartial}}). When set to true, helpers and partials are registered under their full paths (e.g. {{> partials/deep/awesomePartial}}), to prevent clobbering after resolving globbed values.
handlebars - a string representing the path to an instance of handlebars (if you don't want to use the bundeled version).require('handlebars')
Note: This __cannot__ be , as that creates a circular reference. You need to pass the path to the instance you want to use, i.e. handlebars: "./node_modules/handlebars"
#### A note on globing
When you specify templates using globs, the values from template are used to create the values for output, for example, if your file structure looks like this
``
|-foo
|-bar.handlebars
|-baz.handlebars
|-bar.json
|-baz.json
and your configuration looks like this
``
files: [{
expand: true,
cwd: './foo/',
src: '*.handlebars',
dest: './foo/',
ext: '.html'
}],
"templateData": "./foo/*.json",
the output would be ./foo/bar.html and ./foo/baz.html
, fix outputInInput
* 0.7.8 - Eli - add outputInInput setting to send outputted files back to their handlebars directory
* 0.7.7 - Uzi - swap out JSON.parse for alce.parse, allowing for (technically invalid) single quoted json
* 0.7.6 - Kristofferson - explicitly check that isGlob is undefined, preventing a false negative on empty strings
* 0.7.5 - Redford - add registerFullPath` option to prevent partial/helper registration clobbering, update README