Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a mustache without using a file?

Tags:

java

mustache

I have the template saved as a string somewhere and I want to create the mustache with that. How can I do this? Or is it doable?

like image 539
iCodeLikeImDrunk Avatar asked Jun 19 '13 19:06

iCodeLikeImDrunk


1 Answers

Here is the method I found:

private String renderMustacheContent() throws IOException {
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache;

    if (type.getTemplate().trim().isEmpty()) {            
        String emailContent = genCpuEmailContent(cmsKey);
        mustache = mf.compile(new StringReader(emailContent), "cpu.template.email");
    } else {
        mustache = mf.compile(type.getTemplate());
    }

    StringWriter writer = new StringWriter();
    mustache.execute(writer, values).flush();

    return writer.toString();
}

So, basically when you just want to render the email from a String template rather than a file, just create the new StringReader with the template.

like image 99
iCodeLikeImDrunk Avatar answered Oct 05 '22 11:10

iCodeLikeImDrunk