How to create Template based files in Java?
Solution 1
Here's how you would do this in my open-sourced template engine, Chunk.
import com.x5.template.Theme;
import com.x5.template.Chunk;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
...
public void writeTemplatedFile() throws IOException
{
Theme theme = new Theme();
Chunk chunk = theme.makeChunk("my_template", "txt");
// replace static values below with user input
chunk.set("name", "Lancelot");
chunk.set("favorite_color", "blue");
String outfilePath = getFilePath();
File file = new File(outfilePath);
FileWriter out = new FileWriter(file);
chunk.render(out);
out.flush();
out.close();
}
my_template.txt (simply place in the classpath in themes/my_template.txt)
My name is {$name}.
My favorite color is {$favorite_color}.
Output:
My name is Lancelot.
My favorite color is blue.
Your template can be made a bit smarter by adding |filters and :defaults to your tags.
my_template.txt - example 2
My name is {$name|defang:[not provided]}.
My favorite color is {$favorite_color|defang|lc:[not provided]}.
In this example, the defang
filter removes any characters that might help form an XSS attack. The lc
filter changes the text to lowercase. And anything after the colon will be output in case the value is null.
There is an Eclipse plugin available for editing Chunk templates directly in the Eclipse IDE. The plugin provides syntax highlighting and an outline view for template documents.
Chunk can do a lot more, take a peek at the docs for a quick tour. Full disclosure: I love Chunk in part because I created it.
Solution 2
There are a ton of Java templating solutions.
- Velocity
- FreeMarker
- Mustache
- etc.
N.B
Updated on November 29, 2020Comments
-
N.B over 3 years
I want to create a template file in java and Im using Eclipse IDE. I want to create a template file such that one program which gets the parameters from the users, it should be able to paste these parameters into the template file and then save it as a separate file. How can I do this ?
Please guide me.
Thanks N.B
-
Tom McClure over 11 yearsupdated to help warn folks that this answer is a shameless self-plug :)
-
alexander about 9 yearseasy to set up! I struggeld a bit with my own path. But finally, got it worked! :) Thank you so much!
-
TheRealChx101 over 3 yearsThey best one I found easy to use was Jtwig (port of Twig from PHP symfony). It has a very good inheritance structure and scripting (also extensible) versus the other ones where you have to embedded the actual code into HTML tags and have to duplicate code because of lack of inheritance. The drawback is that jtwig has been deprecated :/
-
Dave Newton over 3 years@TheRealChx101 The landscape is much as it was a decade ago (delta Thymeleaf, and I suppose Pebble). You're conflating "HTML templating" with "templating"; they're distinct things. There are quantifiable benefits to having "the code" in HTML tags for HTML templating, though, in that they're actual HTML and can be viewed as such (pre-data prototyping). Template inheritance suffers from the same problems as normal OOP inheritance (sometimes more) because HTML. There are a variety of ways you can implement "template inheritance" as well, depending on what you mean.
-
TheRealChx101 over 3 yearsWhen I said code I meant the template's scripting/templating language. For instance thymeleaf requires you to add attributes to HTML elements which is stupid because it's very difficult to override such behavior or add custom functions. Jtwig is just superior in every way
-
Dave Newton over 3 years@TheRealChx101 It's also not stupid because you can use the HTML as-is during prototyping because it's not mixed in with a different language, it's queryable via normal DOM tools (easier for tooling to deal with and introspect), etc. You may not like it, but it's far from "stupid".