nikhil-bhandari / angular-template-asset-pipeline

AngularJs Template module for the Asset Pipeline

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AngularJs Template Asset-Pipeline

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.

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

Gradle / Grails 3

build.gradle
buildscript {
    dependencies {
        classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.0.20'
        classpath "com.craigburke.angular:angular-template-asset-pipeline:2.2.6"
    }
}
dependencies {
	compile "com.craigburke.angular:angular-template-asset-pipeline:2.2.6"
}

Make sure the dependency is specified in both the buildscript and dependencies blocks.

Grails 2

BuildConfig.groovy
plugins {
	runtime ":angular-template-asset-pipeline:2.2.6"
}
Note
If you’re not yet using Asset Pipeline 2.0+, then you can use version 1.4.2 of the plugin.

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:

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.

Example project

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

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.

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:

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

includeSectionInModuleName

If you want to add all the templates to your base app, you can change the includeSectionInModuleName.

With the setting set to false, a file located at /assets/javascripts/my-app/app-section/templates/index.tpl.htm and moduleNameBase = 'myApp'

Will then generate javascript like this:

angular.module('myApp').run(['$templateCache', function($templateCache) {
	$templateCache.put('index.htm', '<h1>Hello World!</h1>');
}]);

In addition to those settings, you can also change the template folder, disable the compression of your HTML templates, or preserve Html comments.

Gradle

In gradle these settings can be changed in your build.gradle

build.gradle
assets {
	configOptions = [
		angular : [
			templateFolder: 'templates',
			moduleNameBase: '',
			compressHtml: true,
			preserveHtmlComments: false,
			includePathInName: true
		]
	]
}

Grails

In Grails these settings can be set in your Config.groovy

Config.groovy
grails {
	assets {
		angular {
			// Defaults
			templateFolder = 'templates'
			moduleNameBase = ''
			compressHtml = true
			preserveHtmlComments = false
			includePathInName = true
		}
	}
}

About

AngularJs Template module for the Asset Pipeline


Languages

Language:Groovy 65.1%Language:Batchfile 34.9%