Using Java 5 enums as Velocity variables

Maksym picture Maksym · Jul 10, 2009 · Viewed 12.1k times · Source

all. I need to use java 5 enum in velocity template, so that I could write something like

public enum Level{
    INFO, ERROR;
}

Velocity template:

#if($var == Level.INFO)
...
#else
...
#end

How can it be done? Thanks in advance.

Answer

Maksym Govorischev picture Maksym Govorischev · Jul 10, 2009

Actually, instead of toString() method it would be better to use name(), as it returns exactly the value of enum and is final hence can't be overriden in future. So in velocity you can use something like

#if($var.name() == "INFO")