Code Monkey home page Code Monkey logo

angular-template-asset-pipeline's Introduction

AngularJs Template Asset-Pipeline

status?branch=master

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.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.

Grails 2

BuildConfig.groovy
plugins {
	runtime ":angular-template-asset-pipeline:2.2.1"
}
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

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
		}
	}
}

angular-template-asset-pipeline's People

Contributors

craigburke avatar dmahapatro avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.