Skip to content

Commit

Permalink
Merge pull request #11 from ericclemmons/10-module-templates
Browse files Browse the repository at this point in the history
Module Templates

Closes #10
  • Loading branch information
ericclemmons committed Mar 5, 2013
2 parents 10d44af + 140b4d1 commit 5f2c81f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/node_modules
/npm-debug.log
/tmp
node_modules
npm-debug.log
tmp
14 changes: 14 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"node": true,
"es5": true
}
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ grunt.loadNpmTasks('grunt-angular-templates');
This plugin provides the grunt task `ngtemplates`, which will allow you to compile your HTML templates into a single JS file,
which preloads `$templateCache` to prevent round-trips to the server.

### Update Grunt:
### Update Grunt

```js
// grunt.js
grunt.initConfig({
ngtemplates: {
app: {
myapp: {
options: { base: 'src/views' },
src: [ 'src/views/**.html' ],
dest: 'dist/templates.js'
Expand All @@ -41,19 +41,46 @@ grunt.initConfig({
});
```

### Add template module to your app
**You should name your sub-target (e.g. `myapp`) after the name of the module the templates will be added to**.


This will generate the following at `dist/templates.js`:

```js
// js/app.js
angular.module('app', [
'app.templates' // Generated by ngtemplates:app
angular.module('myapp').run(['$templateCache', function($templateCache) {
...
]);
}]);
```

### Include Compiled Templates

This can either be done via HTML:

```html
<script src="dist/templates.js"></script>
```

or via your Gruntfile:

```js
concat: {
myapp: {
src: [
'src/js/**/*.js', // MyApp module first
'<%= ngtemplates.myapp.dest %>' // Generated templates
],
dest: 'dist/js/app.js'
}
}
```


## Changelog

### v0.3.0

- **BC break** - Templates are added to an existing module (e.g. `myapp`) rather than being their own `myapp.templates` module to be manually included, thanks to @geddesign. ([#10](https://github.com/ericclemmons/grunt-angular-templates/issues/10))

### v0.2.2

- Fixes
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-angular-templates",
"description": "Grunt build task to concatenate & register your AngularJS templates in the $templateCache",
"version": "0.2.2",
"version": "0.3.0",
"homepage": "https://github.com/ericclemmons/grunt-angular-templates",
"author": {
"name": "Eric Clemmons",
Expand All @@ -22,16 +22,15 @@
],
"main": "Gruntfile.js",
"engines": {
"node": "0.8.x",
"npm": "1.1.x"
"node": ">= 0.8.0"
},
"scripts": {
"test": "./node_modules/.bin/grunt"
},
"devDependencies": {
"grunt": "0.4.0rc7",
"grunt-contrib-jshint": "~0.1.0",
"grunt-contrib-nodeunit": "~0.1.1",
"grunt": "~0.4.0",
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt-cli": "~0.1.6"
},
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion tasks/angular-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function(grunt) {
var compiler = require('./lib/compiler').init(grunt);

grunt.registerMultiTask('ngtemplates', 'Compile AngularJS templates', function() {
var id = this.target + '.templates';
var id = this.target;
var base = grunt.file.expand(this.options().base || '.')[0];
var files = grunt.file.expand(this.files[0].src);
var dest = path.normalize(this.files[0].dest);
Expand Down
2 changes: 1 addition & 1 deletion tasks/lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports.init = function(grunt) {
};

var compile = function(id, base, files, callback) {
var template = 'angular.module("<%= id %>", []).run(["$templateCache", function($templateCache) {\n<%= content %>\n}]);\n';
var template = 'angular.module("<%= id %>").run(["$templateCache", function($templateCache) {\n<%= content %>\n}]);\n';

concat(base, files, function(err, concated) {
var options = {
Expand Down
2 changes: 1 addition & 1 deletion test/expected/multiple.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module("multiple.templates", []).run(["$templateCache", function($templateCache) {
angular.module("multiple").run(["$templateCache", function($templateCache) {

$templateCache.put("multiple/one.html",
"<h1>One</h1>" +
Expand Down
2 changes: 1 addition & 1 deletion test/expected/simple.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module("simple.templates", []).run(["$templateCache", function($templateCache) {
angular.module("simple").run(["$templateCache", function($templateCache) {

$templateCache.put("simple.html",
"Howdy there! \\ Your name is \"{{ name }}\"." +
Expand Down

0 comments on commit 5f2c81f

Please sign in to comment.