用于文本模板的简单 java lib?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6216404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple java lib for text templating?
提问by amorfis
I need to template some email texts. Nothing fancy, just replace something like @name@
with real value. No pictures, no fancy formatting etc.
我需要模板一些电子邮件文本。没什么特别的,只是@name@
用真正的价值替换类似的东西。没有图片,没有花哨的格式等。
What java lib could you recommend? The simplier the better.
你能推荐什么java lib?越简单越好。
采纳答案by stevevls
You can give Velocityor Freemarkera shot. I've used both in email templating engines. They provide simple syntax for basic use cases, but you can get pretty complex later on!
你可以试试Velocity或Freemarker。我在电子邮件模板引擎中都使用过。它们为基本用例提供了简单的语法,但稍后您可能会变得非常复杂!
Of the two, I personally prefer Freemarker because they've done a really good job of providing all sorts of different builtins that make formatting numbers and text very simple.
在这两者中,我个人更喜欢 Freemarker,因为它们在提供各种不同的内置函数方面做得非常好,这些内置函数使格式化数字和文本变得非常简单。
回答by Matt Ball
A library-free alternative to the libraries already suggested: java.text.MessageFormat
.
已经建议的库的无库替代方案:java.text.MessageFormat
.
回答by Michael Myers
StringTemplateis another option. The five-minute introductiongives some basic examples and syntax.
StringTemplate是另一种选择。在五分钟的介绍了一些基本的例子和语法。
StringTemplate hello = new StringTemplate("Hello, $name$",
DefaultTemplateLexer.class);
hello.setAttribute("name", "World");
System.out.println(hello.toString());
回答by bmscomp
Try Apache Velocity or FreeMarker, they can be helpful, for me I am using FreeMarker
试试 Apache Velocity 或 FreeMarker,它们会有所帮助,对我来说我正在使用 FreeMarker
回答by highlycaffeinated
Have you tried Apache Velocity?
你试过Apache Velocity吗?
回答by Op De Cirkel
It very simple to do it yourself:
自己做很简单:
public class Substitution {
public static void main(String[] args) throws Exception {
String a = "aaaa@bbb@ccc";
// This can be easiliy FileReader or any Reader
Reader sr = new StringReader(a);
// This can be any Writer (ie FileWriter)
Writer wr = new StringWriter();
for (;;) {
int c = sr.read();
if (c == -1) { //EOF
break;
}
if (c == '@') {
String var = readVariable(sr);
String val = getValue(var);
wr.append(val);
}
else {
wr.write(c);
}
}
}
/**
* This finds the value from Map, or somewhere
*/
private static String getValue(String var) {
return null;
}
private static String readVariable(Reader sr)throws Exception {
StringBuilder nameSB = new StringBuilder();
for (;;) {
int c = sr.read();
if (c == -1) {
throw new IllegalStateException("premature EOF.");
}
if (c == '@') {
break;
}
nameSB.append((char)c);
}
return nameSB.toString();
}
}
You have to polish it a little bit, but that's all.
你必须稍微打磨一下,但仅此而已。
回答by Edwin Buck
Agreed, Apache's Velocity is a good call for embedded situations.
同意,Apache 的 Velocity 非常适合嵌入式情况。
If you want a stand-alone product, you can also use Apache's Ant. It reduces the template within a copy task by applying substitution filters.
如果你想要一个独立的产品,你也可以使用 Apache 的 Ant。它通过应用替换过滤器来减少复制任务中的模板。