The angular-template-asset-pipeline
is an Asset Pipeline module that provides angular template precompiler support for Gradle and Grails projects.
Note
|
Starting with version 2.2.0 the default for the config setting includePathInName is true. |
Make sure your templates are contained within a templates folder within the assets folder and have the file extension .tpl.htm or .tpl.html
buildscript {
dependencies {
classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.0.20'
classpath "com.craigburke.angular:angular-template-asset-pipeline:2.2.1"
}
}
dependencies {
compile "com.craigburke.angular:angular-template-asset-pipeline:2.2.1"
}
Make sure the dependency is specified in both the buildscript and dependencies blocks.
This plugin inserts the compressed contents of your template files into AngularJs’s $templateCache. Both the template name and module are determined by the file name and location. This plugin expects the module name to be in camel case (ex. myApp not MyApp).
For example a file located at
/assets/javascripts/my-app/app-section/templates/index.tpl.htm
Will generate javascript like this:
angular.module('myApp.appSection').run(['$templateCache', function($templateCache) {
$templateCache.put('/my-app/app-section/index.htm', '<h1>Hello World!</h1>');
}]);
This allows you to reference your template by just using the path (without the .tpl).
Note
|
this requires that the module myApp.appSection was previously defined in your JavaScript. |
Here’s an example of how you might use this plugin in a project.
//= require /angular/angular
//= require /angular/angular-route
//= require_self (1)
//= require_tree /my-app/app-section/templates
angular.module('myApp.appSection', ['ngRoute'])
.config(function($routeProvider) {
$routeProvider
.when('/index', {
templateUrl: '/my-app/app-section/index.htm'
})
.otherwise({redirectTo: '/index'});
});
-
The require_self is needed to make sure that the myApp.appSection module is defined before the template files are imported.
You can set the moduleBaseName property that will set the base of each calculated module name.
For example if we set the value to myApp then a file located at:
/assets/javascripts/app-section/templates/index.tpl.htm
Will then generate javascript like this:
angular.module('myApp.appSection').run(['$templateCache', function($templateCache) {
$templateCache.put('/app-section/index.htm', '<h1>Hello World!</h1>');
}]);
Note that myApp. was added to the front of the module name.
If you want to refer to a template but just its file name, you can change the includePathInName.
With the setting set to false, a file located at
/assets/javascripts/my-app/app-section/templates/index.tpl.htm
Will then generate javascript like this:
angular.module('myApp.appSection').run(['$templateCache', function($templateCache) {
$templateCache.put('index.htm', '<h1>Hello World!</h1>');
}]);
Warning
|
it’s important to make sure you file name are unique to avoid collisions |
In addition to those settings, you can also change the template folder, disable the compression of your HTML templates, or preserve Html comments.
In gradle these settings can be changed in your build.gradle
assets {
configOptions = [
angular : [
templateFolder: 'templates',
moduleNameBase: '',
compressHtml: true,
preserveHtmlComments: false,
includePathInName: true
]
]
}