Use ternary operator in freemarker?

Jack Hu picture Jack Hu · Jul 9, 2013 · Viewed 37k times · Source

I just want to do something like this:

<a href="${ a? 'a.htm' : 'b.htm'}">

Answer

obourgain picture obourgain · Jul 9, 2013

If you're using freemarker 2.3.23 or newer, you can use the then built-in:

<a href="${a?then('a.htm','b.html')}" target="${openTarget}">

If you're using an older version of freemarker, you can use instead the string built-in:

<a href="${a?string('a.htm','b.html')}" target="${openTarget}">

When applied to a boolean, the string built-in will act as a ternary operator.