Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch over enum in freemarker

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?

like image 416
itsadok Avatar asked Aug 17 '11 08:08

itsadok


1 Answers

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>
...
like image 175
ddekany Avatar answered Sep 20 '22 17:09

ddekany