I want to create files in different formats in the java based web app. The files are pure text files without any html tags. But the files need a certain kind of format, text in left, middle, right or somewhere (tabs, spaces) in the file.
Template with the place holder + java bean with data = text file.
What java template engine or api is fit for this?
Thank you!
Apache FreeMarker™ is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data.
A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.
FreeMarker is a Java-based template engine which can be used in stand-alone or servlet-based Java programs. In FreeMarker you define templates, which are text files that contain the desired output, except that they contain placeholders like ${name} , and even some logic like conditionals, loops, etc.
You may check String Template (GitHub).
It looks like this:
import org.antlr.stringtemplate.*;
import org.antlr.stringtemplate.language.*;
StringTemplate hello = new StringTemplate("Hello, $name$", DefaultTemplateLexer.class);
hello.setAttribute("name", "World");
System.out.println(hello.toString());
Velocity is a pretty flexible templating engine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With