I'm using MessageFormat to format some addresses with a template like this:
我正在使用MessageFormat使用这样的模板格式化一些地址:
"{0}\n{1}\n{2}\n{3}, {4} {5}"
where
- 0 = street 1
- 1 = street 2
- 2 = street 3
- 3 = city
- 4 = state
- 5 = zip
0 =街道1
1 =街道2
2 =街道3
3 =城市
4 =状态
5 =拉链
Most of these fields are optional when captured. How do I avoid having an empty line when for instance, there is not street 3? I could use a template like this "{0}{1}{2}{3}{4}{5}" and then substitute either "street 3\n" or "" accordingly, but then the template doesn't provide much benefit toward formatting.
捕获时,大多数这些字段都是可选的。例如,如果没有街道3,我该如何避免空行?我可以使用像这样的模板“{0} {1} {2} {3} {4} {5}”,然后相应地替换“street 3 \ n”或“”,但模板不会提供对格式化有很大好处。
Is there a way to include conditionals in the format string? Or is there another, preferably lightweight, templating tool available that supports conditional formatting?
有没有办法在格式字符串中包含条件?或者是否有另一种,最好是轻量级的模板工具,它支持条件格式化?
8 个解决方案
#1
7
EDIT: rewrite...
If you don't care about design, then you can readily pick a template engine at random, or because you like its name, or whatever. If you don't have criteria for selecting an engine, then who cares which one you pick?
如果你不关心设计,那么你可以随意选择模板引擎,或者因为你喜欢它的名字,或者其他什么。如果您没有选择发动机的标准,那么谁在乎您选择哪一个?
On the other hand, if you do care about design, such as in using the Model-View-Controller (MVC) design pattern, then your choices quickly diminish.
另一方面,如果您关心设计,例如使用模型 - 视图 - 控制器(MVC)设计模式,那么您的选择会迅速减少。
Most of the answers here emphasize the power of the various template engines. But the whole point of MVC is that you don't want to do more, because doing more in your templates will eventually hurt you very bad. Business logic does not belong in the View, it belongs in the Model. Control logic belongs in the controller. There is only one template engine that actually enforces the MVC pattern. If you don't desire the MVC pattern (perhaps you are moving beyond it), that one engine still helps you to not hurt yourself and encourages you to partition your functionality properly.
这里的大多数答案都强调了各种模板引擎的强大功能。但MVC的重点在于你不想做更多,因为在你的模板中做更多事情最终会让你非常伤心。业务逻辑不属于View,它属于Model。控制逻辑属于控制器。实际上只有一个模板引擎强制执行MVC模式。如果你不想要MVC模式(也许你正在超越它),那一个引擎仍然可以帮助你不伤害自己并鼓励你正确地划分你的功能。
There is really only one good template engine: StringTemplate. See http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf for details of why.
实际上只有一个好的模板引擎:StringTemplate。有关原因的详细信息,请参见http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf。
I have used it on multiple platforms (Python, Java, .Net) as well as many of the alternatives, and StringTemplate rules.
我在多个平台(Python,Java,.Net)以及许多替代方案和StringTemplate规则上使用过它。
Decision done. Enjoy. Best wishes.
做出决定。请享用。最好的祝愿。
#2
5
Others have mentioned string template, but we recently switched to MVEL (which also does more, but really doesn't add much weight): http://mvel.codehaus.org/ (and I find it easier to use).
其他人提到了字符串模板,但我们最近转向MVEL(它也做了更多,但实际上并没有增加太多的重量):http://mvel.codehaus.org/(我发现它更容易使用)。
#3
3
IMHO Chunk Templating engine is the best. The jar file only has 180 KB! and support IF and iteration. How cool is that !
恕我直言Chunk模板引擎是最好的。 jar文件只有180 KB!并支持IF和迭代。多么酷啊 !
#4
2
Don't know how lightweight you'd consider it but Apache Velocity is one of the best-known templating engines for Java.
不知道你认为它有多轻量级,但Apache Velocity是最着名的Java模板引擎之一。
#5
2
This was used to build the site jGuru -- it's been around for a while now.
这被用来建立网站jGuru - 它已经存在了一段时间了。
#6
1
You could just do the formatting the way you're doing it and then remove blank lines by replacing a run of more than one '\n'
s with a single '\n'
, e.g.
你可以按照你的方式进行格式化,然后通过用一个'\ n'替换多个'\ n'的运行来删除空白行,例如
result = result.replaceAll("\n+", "\n");
#8
0
Freemarker is pretty good. It's light, fast, has conditional formatting, and a tonne of other features.
Freemarker非常棒。它轻巧,快速,具有条件格式,以及其他一些功能。
#1
7
EDIT: rewrite...
If you don't care about design, then you can readily pick a template engine at random, or because you like its name, or whatever. If you don't have criteria for selecting an engine, then who cares which one you pick?
如果你不关心设计,那么你可以随意选择模板引擎,或者因为你喜欢它的名字,或者其他什么。如果您没有选择发动机的标准,那么谁在乎您选择哪一个?
On the other hand, if you do care about design, such as in using the Model-View-Controller (MVC) design pattern, then your choices quickly diminish.
另一方面,如果您关心设计,例如使用模型 - 视图 - 控制器(MVC)设计模式,那么您的选择会迅速减少。
Most of the answers here emphasize the power of the various template engines. But the whole point of MVC is that you don't want to do more, because doing more in your templates will eventually hurt you very bad. Business logic does not belong in the View, it belongs in the Model. Control logic belongs in the controller. There is only one template engine that actually enforces the MVC pattern. If you don't desire the MVC pattern (perhaps you are moving beyond it), that one engine still helps you to not hurt yourself and encourages you to partition your functionality properly.
这里的大多数答案都强调了各种模板引擎的强大功能。但MVC的重点在于你不想做更多,因为在你的模板中做更多事情最终会让你非常伤心。业务逻辑不属于View,它属于Model。控制逻辑属于控制器。实际上只有一个模板引擎强制执行MVC模式。如果你不想要MVC模式(也许你正在超越它),那一个引擎仍然可以帮助你不伤害自己并鼓励你正确地划分你的功能。
There is really only one good template engine: StringTemplate. See http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf for details of why.
实际上只有一个好的模板引擎:StringTemplate。有关原因的详细信息,请参见http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf。
I have used it on multiple platforms (Python, Java, .Net) as well as many of the alternatives, and StringTemplate rules.
我在多个平台(Python,Java,.Net)以及许多替代方案和StringTemplate规则上使用过它。
Decision done. Enjoy. Best wishes.
做出决定。请享用。最好的祝愿。
#2
5
Others have mentioned string template, but we recently switched to MVEL (which also does more, but really doesn't add much weight): http://mvel.codehaus.org/ (and I find it easier to use).
其他人提到了字符串模板,但我们最近转向MVEL(它也做了更多,但实际上并没有增加太多的重量):http://mvel.codehaus.org/(我发现它更容易使用)。
#3
3
IMHO Chunk Templating engine is the best. The jar file only has 180 KB! and support IF and iteration. How cool is that !
恕我直言Chunk模板引擎是最好的。 jar文件只有180 KB!并支持IF和迭代。多么酷啊 !
#4
2
Don't know how lightweight you'd consider it but Apache Velocity is one of the best-known templating engines for Java.
不知道你认为它有多轻量级,但Apache Velocity是最着名的Java模板引擎之一。
#5
2
This was used to build the site jGuru -- it's been around for a while now.
这被用来建立网站jGuru - 它已经存在了一段时间了。
#6
1
You could just do the formatting the way you're doing it and then remove blank lines by replacing a run of more than one '\n'
s with a single '\n'
, e.g.
你可以按照你的方式进行格式化,然后通过用一个'\ n'替换多个'\ n'的运行来删除空白行,例如
result = result.replaceAll("\n+", "\n");
#7
#8
0
Freemarker is pretty good. It's light, fast, has conditional formatting, and a tonne of other features.
Freemarker非常棒。它轻巧,快速,具有条件格式,以及其他一些功能。