Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.
/ Hogan.groovy Public archive

A Groovy port of the Hogan.js compiler for Mustache templates

License

Notifications You must be signed in to change notification settings

plecong/Hogan.groovy

Repository files navigation

Hogan.groovy - A Mustache compiler for Groovy.

Hogan.groovy is a Groovy port of the Hogan.js compiler for the Mustache templating language.

For more information on Mustache, see the manpage and the spec.

Basics

Hogan compiles templates to HoganPage objects, which have a render method.

import com.github.plecong.hogan.Hogan

def data = [ screenName: 'plecong' ]

def template = Hogan.compile('Follow @{{screenName}}.')
def output = template.render(data)

// prints "Follow @plecong."
println output

Features

From Twitter's Hogan page:

Hogan has separate scanning, parsing and code generation phases. This way it's possible to add new features without touching the scanner at all, and many different code generation techniques can be tried without changing the parser.

This project ports the parsing and scanning functionality to Groovy and implements a Groovy code generation phase. This means that the compiled templates are Groovy classes that can be generated ahead of time and compiled. Furthermore, because it is Groovy, you can use Groovy closures as the lambdas.

def data = [
	name: 'Willy',
	wrapped: {
		return { text -> "<b>${text}</b>" }
	}
]

def template = Hogan.compile('{{#wrapped}}{{name}} is awesome.{{/wrapped}}')
def expected = '<b>Willy is awesome.</b>'

assert expected == template.render(data)

There is also an experimental (i.e. untested) JavaScript code generation that should output identical code to the Hogan.js method. This will allow for server-side precompilation without having to load Rhino.

The long term vision is that the Groovy compiled templates can be used on the server and the exact same Mustache templates can be compiled into JavaScript and included into source page for client-side template generation.

Grab and Go (Maven Repositories)

Hogan.groovy is now available in Maven Central.

Stable Release

Maven

Note: If you're pulling this into a Java project to use, you'll need to add Groovy 1.8 to your runtime.

<dependency>
	<groupId>com.github.plecong</groupId>
	<artifactId>hogan-groovy</artifactId>
	<version>3.0</version>
</dependency>

Grab/Grapes

@Grab(group='com.github.plecong', module='hogan-groovy', version='3.0')

Gradle

'com.github.plecong:hogan-groovy:3.0'

Motivation

Mostly, because it could be done. There are other alternatives for compiling Mustache templates on the JVM:

These libraries being implemented in Java only are going to be much faster. However, Hogan.groovy is worth using if:

  • You're using Groovy and would like to use Groovy closures as Mustache lambdas
  • You're already using Hogan.js on the client-side and want the exact same features during server-side rendering including:
    • Recursive templates
    • Template inheritance

Compilation options

All of the compilation options for Hogan.js are valid here, including asString, sectionTags, delimiters, and disableLambda. Along with these there are:

keepGenerated: when compiling the HoganPage class, keep the generated Groovy source file. This option will be used in pre-generating and compiling for inclusion in WAR files.

Issues & Versioning

We will try to match the Hogan.js versioning scheme, i.e. semantic versioning, as much as possible.

License

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

About

A Groovy port of the Hogan.js compiler for Mustache templates

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages