Any lightweight templating solutions in Java with support for conditional formatting?
Asked Answered
C

8

5

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?

Curve answered 6/11, 2008 at 21:38 Comment(0)
A
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.

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.

Advocate answered 20/11, 2008 at 2:49 Comment(0)
I
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).

Insistency answered 16/12, 2008 at 5:0 Comment(1)
second on that - MVEL is great!Goofball
B
3

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

Boutte answered 6/6, 2014 at 15:59 Comment(0)
D
2

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

Dryfoos answered 6/11, 2008 at 21:53 Comment(0)
P
2

jguru StringTemplates

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

Purge answered 6/11, 2008 at 22:7 Comment(0)
C
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.

result = result.replaceAll("\n+", "\n");
Crossways answered 6/11, 2008 at 21:55 Comment(1)
Nice simple way to solve the question asked - IMHO introducing a template framework just to format an address is a bit overkill.Han
C
0

Use Velocity or Freemarker.

Charyl answered 6/11, 2008 at 21:58 Comment(0)
H
0

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

Holohedral answered 8/1, 2009 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.