Simple of Templating by mustache.java

Mustache.js is very popular templating js for web. Today we going to look at java version of mustache.

So for getting started we need a file which will act like a template for our program. For this we a file template.mustache which is simple text for a mail with some place holder in mustache syntax.

template.mustache

Hi {{name}},

Your Account number {{accountNo}} mapped with your aardhar card no. {{ uuid}}
Is {{status}}. Thank you for your assistance.
}}
Congrat!!! Now Enjoy the benefits...
{{/isPassed}}
{{#isFailed}}
Sorry !!! Please try agian
{{/isFailed}}

Thank You
{{sender}}

Now when we have our template file ready. we going to use this file a template publish mail to a person in java. And just for now it is going to print it in console because this example is just to give an idea about mustache.java more comprehensive mustache will be covered in later post.

TestMushache.java


class TestMushache{
   
    public void main(String... args){
     
                 MustacheFactory mf = new DefaultMustacheFactory();
         Mustache mustache = mf.compile("template.mustache");
         System.out.println();
         Map map = new HashMap(){
             {
                 put("name", "TestName");
                 put("accountNo", 123679);
                 put("uuid","101010-101010-10101-0420");
                 put("status", "Failed because invalid information");
                 put("isPassed", 0);
                 put("isFailed", 1);
                 put("sender", "Test Sender");
             }
         };
                mustache.execute(new PrintWriter(System.out), map).flush();
     }

}

Output

The following is output on the console

Hi TestName,

Your Account number 123679 mapped with your aardhar card no. 101010-101010-10101-0420
Is Failed because invalid information. Thank you for your assistance.


Sorry !!! Please try agian


Thank You
Test Sender

Maven

To download jar for the mustache you vist this link.

https://mvnrepository.com/artifact/com.github.spullara.mustache.java/compiler/0.9.2

And the maven dependency is followed below.

    <dependency>
		<groupId>com.github.spullara.mustache.java</groupId>
		<artifactId>compiler</artifactId>
		<version>0.9.2</version>
	</dependency>

Please do comment and share the post.

Share :

Subscribe & Social

Subscribe to this blog via RSS.

Recent Posts

Categories

Java 13

Python 2

Wordpress 1

Ops 3

Shared 3

Angular 1

Web 1

Popular Tags

Java (13) Python (2) Wordpress (1) Ops (3) Angular (1) Web (1)

About

DGStack is web blog by digvijay bhakuni. This blog is about computer programming tips and trick. And some short tutorial and gettings started post about the framework, libriay, api and much more. The blog target mostly open source stuff like java, j2ee, nodeJs, angularJs, spring, struts, mongodb, mean, etc

Disclamer

This website claims no credit for any images posted on this site unless otherwise noted. Images on this blog are copyright to its respectful owners. If there is an image appearing on this blog that belongs to you and do not wish for it appear on this site, please E-mail with a link to said image and it will be promptly removed. And the information is best of my knowledge one can always write comment or mail regarding information is this blog.