Freemarker: an enum in an if statement

Geoffrey De Smet picture Geoffrey De Smet · Sep 14, 2012 · Viewed 15.4k times · Source

In my if statement, I want to compare a variable, which is a JDK 1.5 enum, to an enum literal. For example:

<#if type == ProblemStatisticType.BEST_SOLUTION_CHANGED>
  ...
</#if>

But I get this exception:

freemarker.core.InvalidReferenceException: Expression ProblemStatisticType is undefined on line 430, column 87 in index.html.ftl.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125)
at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)

How can I do that?

Answer

ddekany picture ddekany · Sep 14, 2012

Unfortunately, the FreeMarker language doesn't have the concept of classes... but you can do this:

<#if type.name() == "BEST_SOLUTION_CHANGED">
  ...
</#if>

Or if you trust the toString() for the enum type, the .name() part can be omitted.