Java 中是否有支持条件格式的轻量级模板解决方案?

发布于 2024-07-08 16:13:41 字数 409 浏览 8 评论 0原文

我使用 MessageFormat 使用如下模板来格式化一些地址:

"{0}\n{1}\n{2}\n{3}, {4} {5}"

其中

  • 0 = 街道 1
  • 1 = 街道 2
  • 2 = 街道 3
  • 3 = 城市
  • 4 = 州
  • 5 = zip

捕获时,这些字段中的大多数都是可选的。 例如,当没有街道 3 时,如何避免出现空行? 我可以使用这样的模板“{0}{1}{2}{3}{4}{5}”,然后相应地替换“street 3\n”或“”,但模板不提供对格式化有很大好处。

有没有办法在格式字符串中包含条件? 或者是否有另一个支持条件格式的、最好是轻量级的模板工具?

I'm using MessageFormat to format some addresses with a template like this:

"{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

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.

Is there a way to include conditionals in the format string? Or is there another, preferably lightweight, templating tool available that supports conditional formatting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

失而复得 2024-07-15 16:13:41

编辑:重写...

如果您不关心设计,那么您可以轻松地随机选择一个模板引擎,或者因为您喜欢它的名称,或者其他什么。 如果您没有选择引擎的标准,那么谁在乎您选择哪一个呢?

另一方面,如果您确实关心设计,例如使用模型-视图-控制器 (MVC) 设计模式,那么您的选择很快就会减少。

这里的大多数答案都强调了各种模板引擎的强大功能。 但 MVC 的重点是您不想做更多的事情,因为在模板中做更多的事情最终会对您造成非常严重的伤害。 业务逻辑不属于视图,它属于模型。 控制逻辑属于控制器。 只有一个模板引擎实际上强制执行 MVC 模式。 如果您不想要 MVC 模式(也许您正在超越它),该引擎仍然可以帮助您避免伤害自己,并鼓励您正确划分功能。

实际上只有一个好的模板引擎:StringTemplate。 请参阅http://www.cs.usfca.edu/~parrt/ paper/mvc.templates.pdf 了解原因的详细信息。

我已经在多个平台(Python、Java、.Net)以及许多替代方案和 StringTemplate 规则上使用了它。

决定完成了。 享受。 最好的祝愿。

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.

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.

There is really only one good template engine: StringTemplate. See http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf for details of why.

I have used it on multiple platforms (Python, Java, .Net) as well as many of the alternatives, and StringTemplate rules.

Decision done. Enjoy. Best wishes.

剑心龙吟 2024-07-15 16:13:41

其他人提到了字符串模板,但我们最近转向了 MVEL(它也做了更多的事情,但实际上并没有增加太多重量): http://mvel.codehaus.org/(我发现它更容易使用)。

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).

握住我的手 2024-07-15 16:13:41

恕我直言,块模板引擎是最好的。 jar 文件只有 180 KB! 并支持IF迭代。 多么酷啊 !

IMHO Chunk Templating engine is the best. The jar file only has 180 KB! and support IF and iteration. How cool is that !

就此别过 2024-07-15 16:13:41

不知道您认为它有多轻量,但 Apache Velocity 是最著名的模板引擎之一对于Java。

Don't know how lightweight you'd consider it but Apache Velocity is one of the best-known templating engines for Java.

浅唱ヾ落雨殇 2024-07-15 16:13:41

jguru StringTemplates

这用于构建网站 jGuru —— 它已被已经有一段时间了。

jguru StringTemplates

This was used to build the site jGuru -- it's been around for a while now.

Oo萌小芽oO 2024-07-15 16:13:41

您可以按照自己的方式进行格式化,然后通过将多个 '\n' 替换为单个 '\n',例如

result = result.replaceAll("\n+", "\n");

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.

result = result.replaceAll("\n+", "\n");
浅笑轻吟梦一曲 2024-07-15 16:13:41

使用 Velocity 或 Freemarker

Use Velocity or Freemarker.

南烟 2024-07-15 16:13:41

Freemarker 相当不错。 它轻便、快速、具有条件格式以及大量其他功能。

Freemarker is pretty good. It's light, fast, has conditional formatting, and a tonne of other features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文