Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

This plug-in supports using Handlebars.js templates with the Grails Resources Plugin.

branch: master
Octocat-spinner-32 grails-app update to 1.3. March 11, 2014
Octocat-spinner-32 scripts initial commit June 07, 2012
Octocat-spinner-32 src update to 1.3. March 11, 2014
Octocat-spinner-32 test update to 1.3. March 11, 2014
Octocat-spinner-32 web-app update to 1.3. March 11, 2014
Octocat-spinner-32 wrapper add wrapper December 12, 2012
Octocat-spinner-32 .gitignore update to 1.3. March 11, 2014
Octocat-spinner-32 HandlebarsResourcesGrailsPlugin.groovy update to 1.3. March 11, 2014
Octocat-spinner-32 LICENSE.txt add license June 16, 2012
Octocat-spinner-32 README.md update readme for 1.0. July 25, 2013
Octocat-spinner-32 application.properties Make precompiler a spring bean January 02, 2013
Octocat-spinner-32 changelog.md add 1.0.1. September 23, 2013
Octocat-spinner-32 grailsw add wrapper December 12, 2012
Octocat-spinner-32 grailsw.bat add wrapper December 12, 2012
README.md

Handlebars.js Resources plug-in for Grails

This plug-in supports using Handlebars.js templates with the Grails Resources Plugin. This plugin includes a resource mapper that will precompile the template files into JavaScript and make it available for other resource processing, including minification and bundling. The Handlebars.js resource files are also included.

Build Status

Usage

Declaring Resources

application {
    dependsOn 'handlebars_runtime'
    resource url: 'templates/person.handlebars', attrs: [type: 'js'], bundle:'bundle_application'
    resource url: 'templates/error.handlebars', attrs: [type: 'js'], bundle:'bundle_application'
    resource url:'js/application.js'
}

Settings

  • dependsOn: handlebars or handlebars_runtime. If only using precompiled templates the smaller handlebars_runtime should be used.
  • url: location of the handlebars template file.
  • attrs[type]: must be set to js. If the file extension is .handlebars or .hbs this setting is not necessary.
  • bundle: must be set as will not default correctly. To add to default bundle use bundle_<module name>.

Using in the Browser

Template functions are stored in the Handlebars.templates object using the template name. If the template name is person/show, then the template function can be accessed from Handlebars.templates['person/show']. See the Template Names section for how template names are calculated.

See the Handlebars.js website for more information on using Handlebars template functions.

Template Names

Template names are based on the resource URL. If the URL is templates/foo.handlebars, then the template name will be templates/foo. Note that the extension is removed.

The default path separator is /. If you want to change it, you can specify a value for templatesPathSeparator in the configuration. For example, adding

grails.resources.mappers.handlebars.templatesPathSeparator = '.'

will change the template name to templates.foo.

If you specify a value for templatesRoot in the configuration, then that value will be stripped from the template name. For example, adding

grails.resources.mappers.handlebars.templatesRoot = 'templates'

will change the template name to just foo.

Configuration

All configuration variables should be relative to:

grails.resources.mappers.handlebars
  • templatesRoot: The root folder of the templates relative to web-app. This value will be stripped from template paths when calculating the template name. Default is none.
  • templatesPathSeparator: The delimiter to use for template names. Default is /
  • includes: The file patterns to include. Default is ['**/*.handlebars', "**/*.hbs"]
  • extraKnownHelpers: List of extra helpers that are available at runtime. Default is none.
  • wrapTemplate: Closure that determines how the template is wrapped. Takes the template name and compiled template as arguments. Useful if you want to change or optimize the variable assignments. Default is:

    { String templateName, String compiledTemplate ->
        """
    (function(){
        var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
        templates['$templateName'] = template($compiledTemplate);
    }());
    """
    }
    
Something went wrong with that request. Please try again.