larsonjj/grunt-dashboard — pull request #2 by markbrouch (2014-10-29 17:13:31+00:00)
Title: Added ability to pass custom variables to Handlebars template from Grunt task.

I needed a way to render the current version on the dashboard page, so I created a new option for passing user defined variables from the Grunt task to the Handlebars file. My new Grunt task configuration looks like this:

````
{
server: {
options: {
dashTemplate: '<%= yeogurt.dev %>/dashboard/template.hbs',
htmlTemplate: '<%= yeogurt.dev %>/dashboard/htmlTemplate.hbs',
logo: 'images/logo.png',
generatedDir: '<%= yeogurt.server %>/dashboard/generated',
userData: {
build: 'build.<%= svninfo.rev %>.zip',
},
assets: [{
cwd: '<%= yeogurt.dev %>/views/components/',
src: [
'**/*',
'!*.hbs'
]
}]
},
files: {
'<%= yeogurt.server %>/dashboard/index.html': [
'<%= yeogurt.dev %>/views/**/*.jade'
]
}
},
dist: {
options: {
dashTemplate: '<%= yeogurt.dev %>/dashboard/template.hbs',
htmlTemplate: '<%= yeogurt.dev %>/dashboard/htmlTemplate.hbs',
logo: 'images/logo.png',
generatedDir: '<%= yeogurt.dist %>/dashboard/generated',
userData: {
build: 'build.<%= svninfo.rev %>.zip',
},
assets: [{
cwd: '<%= yeogurt.dev %>/views/components/',
src: [
'**/*',
'!*.hbs'
]
}]
},
files: {
'<%= yeogurt.dist %>/dashboard/index.html': [
'<%= yeogurt.dev %>/**/*.jade'
]
}
}
}
````

Notice the extra `userData` option, and how I'm using it to pass Grunt config variables to the Handlebars template. In this example, I can access a string that has the build version zip file in the Handlebars template by using `{{userData.build}}`.