Precompile ember templates in grunt using only headless-ember.js and ember.js.
-
Install this grunt plugin next to your project's grunt.js gruntfile with:
npm install grunt-ember-handlebars -
Then add this line to your project's
grunt.jsgruntfile:
grunt.loadNpmTasks('grunt-ember-handlebars');In your gruntfile.js:
grunt.initConfig({
// Compile ember templates:
ember_handlebars: {
all: {
// In practice, this could be:
// src: ['templates/**/*.hbs', 'templates/**/*.handlebars']
src: ['templates/main.hbs', 'templates/post.handlebars'],
dest: 'templates/compiled'
}
},
// Include the templates in your app:
concat: {
all: {
src: ['templates/compiled/*.js', 'app.js'],
dest: 'public/myapp.js'
}
}
});
// Load the plugin. This assumes you have installed it via NPM.
grunt.loadNpmTasks('grunt-ember-handlebars');
grunt.registerTask('default', ['ember_handlebars', 'concat']);Run the task using grunt, and grunt-ember-handlebars will compile and include
the templates using
Ember.TEMPLATES.
Now, just use them in your views:
App.MainView = Em.View.extend({
templateName: 'main'
});
App.PostView = Em.View.extend({
templateName: 'post'
});
// etc.That's it!
In lieu of a formal styleguide, take care to maintain the existing coding style.
v0.4 - Updated ember.js library we're using, since it looks like precompilation has been updated.
v0.1 - Woo!
Copyright (c) 2012 Mukund Lakshman
Licensed under the MIT license.
