Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access Enum fields in playframework template

I have an Enum which has certain fields ,which I need to access in my play1.2.4 template

public Enum WORKER{
   FARMER,SMITH,GARDENER
}

Suppose I need to check if a 'person' variable in the template is a farmer,smith or gardener ,how can I do it?

#{if person.Type==WORKER.FARMER}
   ...do something...
#{/if}

Here I get

NullPointerException : Cannot get property 'FARMER' on null object.

So,the template doesn't know about the Enum WORKER.Since a new instance cannot be created for Enum ,how should I make the Enum available to the template?

like image 941
Damon Julian Avatar asked Mar 01 '12 16:03

Damon Julian


1 Answers

Use the enum's absolute class name in the template. E.g. if your enum WORKER is in the package model.myenums, the template code would look like this:

#{if person.Type == model.myenums.WORKER.FARMER}
   ...do something...
#{/if}
like image 102
Carsten Avatar answered Oct 19 '22 03:10

Carsten