Check if a map is empty in Apache Velocity

John Farrelly picture John Farrelly · Jan 4, 2013 · Viewed 13.7k times · Source

In my java/spring app, I have a velocity template in which I create a map which will hold values also inserted in the template:

#set ($myMap = {})

What I want to do is have an if/else checking if the map is empty. This doesn't seem to be working for me. I've tried:

#if ($myMap.empty)
...
#if ($myMap.size == 0)

Neither of these work. What is the correct way to check if a Map is empty in velocity. I've tried searching the Documentation and SO, but I can't find an example.

Answer

GOTO 0 picture GOTO 0 · Jan 4, 2013

isEmpty and size are methods, so they should be used like this:

#if ($myMap.isEmpty())
...
#if ($myMap.size() == 0)