I thought that switching over an enum would be something very basic in FreeMarker, so that I could write something like:
<!-- Doesn't work -->
Dear
<#switch gender>
<#case MALE>
sir
<#break>
<#case FEMALE>
madam
<#break>
<#default>
sir/madam
<#/switch>
But it seems that accessing enums is an ugly and complicated matter. Is there a proper, clean way to switch on the values of an enum? Should I store it differently in my model?
You could just use the string value of the enums (at least with the default object wrapper, and basically with any other BeansWrapper
variation too):
<#switch gender>
<#case "MALE"> <#-- note the quotes -->
sir
<#break>
...
Or if depending on toString()
is a problem (and it could be), use this:
<#switch gender.name()>
<#case "MALE">
sir
<#break>
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With