Android – Chunk Template Engine Integration

Developers, I hope you all are doing good. Let me show you an interesting library or type of library i.e., Template engine. Template engines are tools to separate program-logic and presentation into two independent parts, this makes the development of both logic and presentation easier, improves flexibility and eases modification and maintenance. There are several template engines available like Apache Velocity, Apache FreeMarker, Thymeleaf, Chunk. In this article let me show you integration of Chunk by x5software

Chunk template engine

What is Chunk ?

Chunk is a Java template engine for application that serve HTML or XML. Chunk is compact and speedy, easy to learn yet very powerful, it’s the little engine that can handle any templating job, plain text, markup, markdown, whatever, wherever. It provides rich features set : in-tag filters, in-tag default values, includes, looping and branching, defining multiple snippets per template file, layered themes, macros, and much much more.

Adding Gradle dependency

To integrate Chunk with Android app, the first and foremost step is to add the gradle depedency. But still there is no direct gradle link for Chunk so to add the Chunk dependency to your project you need to download the jar from this link.

After that add the jar file to the /libs directory and add into the build.gradle file within app module using following snippet.

dependencies {
// add other libraries
compile fileTree(include: ['*.jar'], dir: 'libs')
}

Binding Beans to Template

If you are already using Java beans classes then Chunk provides you an option to bind the beans object directly to the HTML or XML templates. To do so Chunk also depends on another library called madrobotbeans. Again this is also you have to download and place it inside /libs directory.

Read also –

Integration

Now the final step it to integrate Chunk api in you application. To do so create a directory named themes inside assets directory and place you HTML files within themes directory, extension of html files should be .chtml instead of html. Make sure you put {#template_name} at the beginning of the file and {#} at the end of the file.  Here is a simple example of chtml – 

{#example_1}
<div>
 Earth to {$name}.  Come in, {$name}.
</div>
{#}

In the above snippet, {$name} is the placeholder for the name. Similarly you can place your placeholders through out the html document and in your class use the following code –

Theme theme = new Theme("examples");
 
// Fetch template from this file: themes/examples/hello.chtml
// Inside that file there is a template "snippet" named #example_1
Chunk html = theme.makeChunk("hello#example_1");
 
html.set("name", "Bob");
 
html.render( out );
 
// or, render to a string
String output = html.toString();

 

In the above code output variable will get the newly generated html string by replacing the {$name} with Bob.

 

Documentation wise also this is pretty rich so anyone can easily get comfortable in using it unlike other popular template engines. So in case if your Android app is providing capability to show same data in different templates then Chunk is your best friend to use.

 

I hope this article will help you, so do share this with your friends and colleagues.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *