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.
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.
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();
}
}
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
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 to this blog via RSS.
Java 14
Python 2
Ops 3
Shared 3
Angular 1
Web 1
Java (14) Python (2) Wordpress (1) Ops (3) Angular (1) Web (1)