Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the java template engine/api for creating a text file?

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!

like image 742
Leon Avatar asked Sep 13 '12 13:09

Leon


People also ask

What is a template engine in Java?

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.

What are template engines used for?

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.

What is FreeMarker template?

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.


2 Answers

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());
like image 108
ElderMael Avatar answered Sep 21 '22 14:09

ElderMael


Velocity is a pretty flexible templating engine.

like image 40
jtahlborn Avatar answered Sep 20 '22 14:09

jtahlborn