Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

http://jknack.github.io/handlebars.java/

Logic-less and semantic Mustache templates with Java
http://jknack.github.io/handlebars.java/

handlebars template-engine templates

Last synced: 3 months ago
JSON representation

Logic-less and semantic Mustache templates with Java

Lists

README

        

[![Become a Patreon](https://img.shields.io/badge/patreon-donate-orange.svg)](https://patreon.com/edgarespina)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.jknack/handlebars/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.jknack/handlebars)
[![javadoc](https://javadoc.io/badge/com.github.jknack/handlebars.svg)](https://javadoc.io/doc/com.github.jknack/handlebars)

Handlebars.java
===============
## Logic-less and semantic Mustache templates with Java

```java
Handlebars handlebars = new Handlebars();

Template template = handlebars.compileInline("Hello {{this}}!");

System.out.println(template.apply("Handlebars.java"));
```

Output:
```
Hello Handlebars.java!
```

Handlebars.java is a Java port of [handlebars](https://handlebarsjs.com/).

Handlebars provides the power necessary to let you build semantic templates effectively with no frustration.

[Mustache](https://mustache.github.io/mustache.5.html) templates are compatible with Handlebars, so you can take a [Mustache](https://mustache.github.io) template, import it into Handlebars, and start taking advantage of the extra Handlebars features.

# Requirements

- Handlebars 4.4+ requires Java 17 or higher.
- Handlebars 4.3+ requires Java 8 or higher (NOT MAINTAINED).

# Getting Started
In general, the syntax of **Handlebars** templates is a superset of [Mustache](https://mustache.github.io) templates. For basic syntax, check out the [Mustache manpage](https://mustache.github.io).

The [Handlebars.java blog](https://jknack.github.io/handlebars.java) is a good place for getting started too. Javadoc is available at [javadoc.io](https://javadoc.io/doc/com.github.jknack/handlebars).

## Maven
#### Stable version: [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.jknack/handlebars/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.jknack/handlebars)

```xml

com.github.jknack
handlebars
${handlebars-version}

```

### Loading templates
Templates are loaded using the ```TemplateLoader``` class. Handlebars.java provides three implementations of a ```TemplateLoader```:
* ClassPathTemplateLoader (default)
* FileTemplateLoader
* SpringTemplateLoader (see the [handlebars-springmvc](https://github.com/jknack/handlebars.java/tree/master/handlebars-springmvc) module)

This example loads ```mytemplate.hbs``` from the root of the classpath:

mytemplate.hbs:
```
Hello {{this}}!
```

```java
var handlebars = new Handlebars();

var template = handlebars.compile("mytemplate");

System.out.println(template.apply("Handlebars.java"));
```

Output:
```
Hello Handlebars.java!
```

You can specify a different ```TemplateLoader``` by:

```java
TemplateLoader loader = ...;
Handlebars handlebars = new Handlebars(loader);
```

#### Templates prefix and suffix
A ```TemplateLoader``` provides two important properties:
* ```prefix```: useful for setting a default prefix where templates are stored.
* ```suffix```: useful for setting a default suffix or file extension for your templates. Default is: ```.hbs```

Example:
```java
TemplateLoader loader = new ClassPathTemplateLoader();
loader.setPrefix("/templates");
loader.setSuffix(".html");
Handlebars handlebars = new Handlebars(loader);

Template template = handlebars.compile("mytemplate");

System.out.println(template.apply("Handlebars.java"));
```

Handlebars.java will resolve ```mytemplate``` to ```/templates/mytemplate.html``` and load it.

## The Handlebars.java Server
The handlebars.java server is small application where you can write Mustache/Handlebars template and merge them with data.

It is a useful tool for Web Designers.

Download from Maven Central:

1. Go [here](http://search.maven.org/#search%7Cga%7C1%7Chandlebars-proto)
2. Under the **Download** section click on **jar**

Maven:
```xml

com.github.jknack
handlebars-proto
${current-version}

```

Usage:
```java -jar handlebars-proto-${current-version}.jar -dir myTemplates```

Example:

**myTemplates/home.hbs**

```


    {{#items}}
    {{name}}
    {{/items}}

```

**myTemplates/home.json**

```json
{
"items": [
{
"name": "Handlebars.java rocks!"
}
]
}
```

or if you prefer YAML **myTemplates/home.yml**:

```yml
items:
- name: Handlebars.java rocks!
```

### Open a browser a type:
```
http://localhost:6780/home.hbs
```
enjoy it!

### Additional options:

* -dir: set the template directory
* -prefix: set the template's prefix, default is /
* -suffix: set the template's suffix, default is .hbs
* -context: set the context's path, default is /
* -port: set port number, default is 6780
* -content-type: set the content-type header, default is text/html

### Multiple data sources per template
Sometimes you need or want to test multiple datasets over a single template, you can do that by setting a ```data``` parameter in the request URI.

Example:

```
http://localhost:6780/home.hbs?data=mytestdata
```
Please note you don't have to specify the extension file.

## Helpers

### Built-in helpers:
* **with**
* **each**
* **if**
* **unless**
* **log**
* **block**
* **partial**
* **precompile**
* **embedded**
* **i18n** and **i18nJs**
* **string helpers**
* **conditional helpers**

### with, each, if, unless:
See the [built-in helper documentation](https://handlebarsjs.com/guide/block-helpers.html).

### block and partial
Block and partial helpers work together to provide you [Template Inheritance](http://jknack.github.io/handlebars.java/reuse.html).

Usage:
```
{{#block "title"}}
...
{{/block}}
```
context: A string literal which defines the region's name.

Usage:
```
{{#partial "title"}}
...
{{/partial}}
```
context: A string literal which defines the region's name.

### precompile
Precompile a Handlebars.java template to JavaScript using handlebars.js

user.hbs

```html
Hello {{this}}!
```

home.hbs

```html

{{precompile "user"}}

```

Output:
```html

(function() {
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['user'] = template(function (Handlebars,depth0,helpers,partials,data) {
helpers = helpers || Handlebars.helpers;
var buffer = "", functionType="function", escapeExpression=this.escapeExpression;

buffer += "Hi ";
depth0 = typeof depth0 === functionType ? depth0() : depth0;
buffer += escapeExpression(depth0) + "!";
return buffer;});
})();

```
You can access the precompiled template with:

```js
var template = Handlebars.templates['user']
```

By default it uses: ```/handlebars-v1.3.0.js``` to compile the template. Since handlebars.java 2.x it is also possible to use handlebars.js 2.x

```java
Handlebars handlebars = new Handlebars();
handlebars.handlebarsJsFile("/handlebars-v2.0.0.js");
```

For more information have a look at the [Precompiling Templates](https://github.com/wycats/handlebars.js/) documentation.

Usage:
```
{{precompile "template" [wrapper="anonymous, amd or none"]}}
```
context: A template name. Required.

wrapper: One of "anonymous", "amd" or "none". Default is: "anonymous"

There is a [maven plugin](https://github.com/jknack/handlebars.java/tree/master/handlebars-maven-plugin) available too.

### embedded
The embedded helper allow you to "embedded" a handlebars template inside a `````` HTML tag:

user.hbs

```html
<tr>
<td>{{firstName}}</td>
<td>{{lastName}}</td>
</tr>
```

home.hbs

```html
<html>
...
{{embedded "user"}}
...
</html>
```
Output:
```html
<html>
...
<script id="user-hbs" type="text/x-handlebars">
<tr>
<td>{{firstName}}</td>
<td>{{lastName}}</td>
</tr>

...