prabello prabello - 1 year ago 78
Java Question

How to get mustache html in java

I'm trying to use mustache to fill an html for me, then i want to get that html and use it as String.

A template like this -> template.xhtml

<table style="width:10%" align="center">
<tr>
<th>Old</th>
<th>New</th>
</tr>
<tr>
<td>{{old}}</td>
<td>{{new}}</td>
</tr>
</table>


With a Hash like this:

HashMap<String, String> scopes = new HashMap<String, String>();
scopes.put("old", "Testing");
scopes.put("new", "Mustache");


Now how do i tell, Mustache to use template.xhtml and fill with scopes and then return me the html?

Answer Source
String aux = "";    
MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile("path/to/file.html");
    StringWriter stringWriter = new StringWriter();
    mustache.execute(stringWriter, wrapper);
    aux = stringWriter.toString();
    System.out.println(aux);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download