Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Handlebars Java as a Server Side Templating Language

DZone's Guide to

Using Handlebars Java as a Server Side Templating Language

The disconnect between a programming language and how media is rendered has always caused problems. Handlebars is one of the best solutions on the market right now for Java devs building web apps.

Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

<span ng-non-bindable>{{ Handlebars.java }}</span> - Mustache templates in Java

              handlebars.java is a server side Java template engine like Velocity or Freemaker. It follows the syntax of the Mustache spec. The main goal of handlebar.java is to reuse the same templates on both the client and server side. In this article we will see how to use Handlebars.java along with the Spring framework. I have used Spring Boot for this demonstration. Follow the below steps to setup and run handlebars.java

Step 1

Include the handlebars.java implementation in the pom.xml

<dependency>
    <groupId>com.github.jknack</groupId>
    <artifactId>handlebars</artifactId>
    <version>4.0.5</version>
</dependency>

As we are going to use JSON as the data to be rendered with handlebars template, we will include the handlebars JSON helper in the class path.

<dependency>
    <groupId>com.github.jknack</groupId>
    <artifactId>handlebars-jackson2</artifactId>
    <version>4.0.5</version>
</dependency>
<dependency>
    <groupId>com.github.jknack</groupId>
    <artifactId>handlebars-guava-cache</artifactId>
    <version>4.0.5</version>
</dependency>

Step 2

Write handlebar template to render home page. Here, I divided the home page template into header and footer and included them as partials.

{{>header/header}}
This is the sample template to demonstrate the Handlebars JAVA. This uses JSON to render.
{{>footer/footer address}}

The header template is given below.

<h1>Hi, This is {{title}} {{name}}</h1>

The footer template is given below.

The address is given below.
<h4>address1:{{address1}}</h4>
<h4>address2:{{address2}}</h4>
<h4>city:{{city}}</h4>
<h4>state :{{state}}</h4>

Step 3

As we are using Spring Boot, I kept the templates under resources/templates folder. Load the templates from the calsspath.

TemplateLoader loader = new ClassPathTemplateLoader("/templates", ".hbs");
final Cache<TemplateSource, Template> templateCache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(1000).build();
setHandlebars(new Handlebars(loader).with((new GuavaTemplateCache(templateCache))));

Step 4

Compile the template and apply the data to render.

Template template = this.getHandlebars().compile(templateName);
//Sample JSON to render the home template
String responseJson = "{\"title\":\"Mr.\",\"name\":\"ABC\",\"address\":{\"address1\":\"address1\",\"address2\":\"address2\",\"city\":\"XYZ\",\"state\":\"State1\"}}";
JsonNode jsonNode = new ObjectMapper().readValue(responseJson, JsonNode.class);
//get the compiled template
Template template = handlebarsDemoTemplateLoader.getTemplate("home");
//Apply the JSON data by passing the context
template.apply(handlebarsDemoTemplateLoader.getContext(jsonNode));

I included the above code in org.smarttechie.controller.HandlebarsDemoController . Just run the org.smarttechie.HandlebarsjavaDemoApplication and send the request to  /demo/home to see the rendered home page handlebar template. The source code created for this example is available on GitHub. Download it and explore more about the handlebars java implementation.

Spring Boot Handlebars Java Demonstration

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
template ,java ,mustache ,json ,handlebars

Published at DZone with permission of Siva Prasad Rao Janapati, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}