How to create a mustache without using a file?

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

+4
source share
2 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 display email from a String template and not from a file, just create a new StringReader using the template.

+10
source

If you store this line in a file using FileWriter (or some other classes) and save the .mustache extension, you should be able to close and use this file. Here's something I quickly hacked (this might not work):

 //write to the file FileUtils.writeStringToFile(new File("template.mustache"), "example mustache stuff"); //do stuff.... //code for turning template into mustache MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile("template.mustache"); mustache.execute(new PrintWriter(System.out), new Example()).flush(); 

EDIT: I am not reading the title correctly. You can create a temporary file and then delete it later.

Sources:

How to save a string to a text file using Java?

https://github.com/spullara/mustache.java/blob/master/example/src/main/java/mustachejava/Example.java

+1
source

Source: https://habr.com/ru/post/1487119/


All Articles