Skip to content

Commit

Permalink
Precompiling ejs templates
Browse files Browse the repository at this point in the history
* will fix the issue #1
* All ejs files in app/scripts/templates directory will be precompile to scripts/templates.js
* Uses grunt-contrib-jst
  • Loading branch information
revathskumar committed Apr 2, 2013
1 parent 8d58695 commit 2ca91dd
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
7 changes: 6 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ Generator.prototype.mainStylesheet = function mainStylesheet() {
}
};

Generator.prototype.jstTemplates = function jstTemplates() {
this.copy('templates.js', 'app/scripts/templates.js');
};

Generator.prototype.writeIndex = function writeIndex() {
// prepare default content text
var defaults = ['HTML5 Boilerplate', 'jQuery', 'Backbone.js', 'Underscore.js', 'Mocha'];
Expand Down Expand Up @@ -138,7 +142,8 @@ Generator.prototype.writeIndex = function writeIndex() {
}

this.indexFile = this.appendScripts(this.indexFile, 'scripts/main.js', [
'scripts/main.js'
'scripts/main.js',
'scripts/templates.js'
]);

// iterate over defaults and create content string
Expand Down
10 changes: 10 additions & 0 deletions app/templates/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ module.exports = function (grunt) {
all: {
rjsConfig: '<%%= yeoman.app %>/scripts/main.js'
}
},
jst: {
compile: {
files: {
'.tmp/scripts/templates.js': ['<%%= yeoman.app %>/scripts/templates/*.ejs']
}
}
}
});

Expand All @@ -243,6 +250,7 @@ module.exports = function (grunt) {
grunt.task.run([
'clean:server',
'coffee:dist',
'jst',
'compass:server',
'livereload-start',
'connect:livereload',
Expand All @@ -254,6 +262,7 @@ module.exports = function (grunt) {
grunt.registerTask('test', [
'clean:server',
'coffee',
'jst',
'compass',
'connect:test',
'mocha'
Expand All @@ -262,6 +271,7 @@ module.exports = function (grunt) {
grunt.registerTask('build', [
'clean:dist',
'coffee',
'jst',
'compass:dist',
'useminPrepare',
'imagemin',
Expand Down
1 change: 1 addition & 0 deletions app/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"grunt-contrib-copy": "~0.4.0",
"grunt-contrib-concat": "~0.1.2",
"grunt-contrib-coffee": "~0.4.0",
"grunt-contrib-jst": "~0.5.0",
"grunt-contrib-uglify": "~0.1.1",
"grunt-contrib-compass": "~0.1.2",
"grunt-contrib-jshint": "~0.1.1",
Expand Down
1 change: 1 addition & 0 deletions app/templates/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this["JST"] = this["JST"] || {};
2 changes: 1 addition & 1 deletion templates/view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= _.camelize(appname) %>.Views.<%= _.classify(name) %>View = Backbone.View.extend({

//template: <%= _.underscored(name) %>
template: JST['<%= jst_path %>']

});
16 changes: 15 additions & 1 deletion test/test-foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ describe('Backbone generator test', function() {
'.jshintrc',
'.editorconfig',
'Gruntfile.js',
'package.json'
'package.json',
'app/scripts/templates.js'
];

helpers.mockPrompt(this.backbone.app, {
Expand Down Expand Up @@ -127,4 +128,17 @@ describe('Backbone generator test', function() {
done();
});
});

describe('Backbone View', function() {
it('creates backbone view', function(done){
var view = helpers.createGenerator('backbone:view',['../../view'], ['foo']);
view.run([], function(){
helpers.assertFiles([
['app/scripts/views/foo-view.js', /Views.FooView = Backbone.View.extend\(\{(.|\n)*app\/scripts\/templates\/foo.ejs/],
'app/scripts/templates/foo.ejs'
]);
});
done();
});
});
});
3 changes: 2 additions & 1 deletion view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ util.inherits(Generator, scriptBase);

Generator.prototype.createViewFiles = function createViewFiles() {
var ext = this.options.coffee ? 'coffee' : 'js';
this.jst_path = path.join('app/scripts/templates', this.name + '.ejs');
this.template('view.ejs', this.jst_path);
this.template('view.' + ext, path.join('app/scripts/views', this.name + '-view.' + ext));
this.template('view.ejs', path.join('app/scripts/templates', this.name + '.ejs'));
this.addScriptToIndex('views/' + this.name + '-view');
};

0 comments on commit 2ca91dd

Please sign in to comment.