All Projects → craigburke → Angular Template Asset Pipeline

craigburke / Angular Template Asset Pipeline

AngularJs Template module for the Asset Pipeline

Programming Languages

groovy
2714 projects

:apVersion: 2.6.11 :version: 2.4.0 = AngularJs Template Asset-Pipeline

The angular-template-asset-pipeline is an https://github.com/bertramdev/asset-pipeline-core[Asset Pipeline] module that provides angular template precompiler support for Gradle projects.

NOTE: Starting with version 2.2.0 the default for the config setting includePathInName is true.

== Getting started

Make sure your templates are contained within a templates folder within the assets folder and have the file extension .tpl.htm or .tpl.html

=== Setup

[source,groovy,subs='attributes'] .build.gradle

buildscript { dependencies { classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:{apVersion}' } }

dependencies { assets "com.craigburke.angular:angular-template-asset-pipeline:{version}" }

== How it works

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: [source,javascript]

angular.module('myApp.appSection').run(['$templateCache', function($templateCache) { $templateCache.put('/my-app/app-section/index.htm', '

Hello World!

'); }]);

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.

== Example project Here's an example of how you might use this plugin in a project.

[source,javascript]

//= 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'}); });

<1> The require_self is needed to make sure that the myApp.appSection module is defined before the template files are imported.

== Configuration

==== moduleBaseName 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:

[source,javascript]

angular.module('myApp.appSection').run(['$templateCache', function($templateCache) { $templateCache.put('/app-section/index.htm', '

Hello World!

'); }]);

Note that myApp. was added to the front of the module name.

==== convertUnderscores The convertUnderscores property controls whether an underscore in a folder name will be translated to a camel case equivalent. For example whether a file at /assets/javascripts/my_app/app_section/foo.tpl.htm will be put into a module name myApp.appSection or my_app.app_section

==== templateModuleName If you set the templateModuleName property than all templates will be placed in a single module (regardless of what the moduleBaseName is set to or what the path of the template file is). For example, if templateModuleName is set to myTemplates then a file located at /assets/javascripts/app-section/templates/index.tpl.htm

Will generate javascript like:

[source,javascript]

angular.module('myTemplates').run(['$templateCache', function($templateCache) { $templateCache.put('/app-section/index.htm', '

Hello World!

'); }]);

==== includePathInName 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:

[source,javascript]

angular.module('myApp.appSection').run(['$templateCache', function($templateCache) { $templateCache.put('index.htm', '

Hello World!

'); }]);

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.

=== Default Settings In gradle these settings can be changed in your build.gradle

[source,groovy] .build.gradle

assets { configOptions = [ angular : [ templateFolder: 'templates', moduleNameBase: '', includePathInName: true, templateModuleName: '', convertUnderscores: true, compressHtml: true, preserveHtmlComments: false, preserveLineBreaks: false ] ] }

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].