Hadlebars.java – How to use it as Server side Template Engine?

{{ Handlebars.java }} - Mustache templates in Java

               handlebars.java is a server-side Java template engine like velocity, freemaker, etc. It follows the syntax of Mustache spec. The main goal of handlebar.java is to reuse the same templates at client side and server side. In this article, we will see how to use Handlebars.java along with Spring framework. I have used Spring Boot for the demonstration. Follow the below steps to set up and run the handlebars.java

Step 1:

Include the handlebars.java implementation in the pom.xml


    com.github.jknack
    handlebars
    4.0.5

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


    com.github.jknack
    handlebars-jackson2
    4.0.5


    com.github.jknack
    handlebars-guava-cache
    4.0.5

Step 2:

Write handlebar template to render a 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 classpath.

TemplateLoader loader = new ClassPathTemplateLoader("/templates", ".hbs");
final Cache 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

Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

Tagged with: , ,
Posted in Java

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dzone.com
DZone

DZone MVB

Java Code Geeks
Java Code Geeks
OpenSourceForYou