Creating a new map object

Since the 6.5.2 version of the Velocity implementation doesn’t allow the syntax for Map object creation, I had to write a work around.

This is the syntax that apache’s Velocity documentation says should work, but doesn’t:

#set( $myMap = {} )

This is the macro I built to work around:

## macro to create a new map object. This is a workaround to a bug in Rhythmyx's velocity implementation.
## Sam Rushing, Feb 3, 2010 
#macro(newMap $obj)##
#set( $obj = "" )##
#set( $obj = $obj.getClass().forName("java.util.HashMap").newInstance() )##

And the syntax I can now use is:

#newMap( $myMap )

Following up, It is also possible to simply replace the velocity java archives with the latest from apache and you’ll open up lots of new functionality. The map object creation being only just 1 such new feature.

I haven’t checked CM System 6.7 yet, but in 6.5.2 I had velocity-1.4.jar and velocity-tools-1.3.jar.

I replaced these with velocity-1.5.jar and velocity-tools-view-1.4.jar.

Thanks for this macro. Another technique for creating a map is:

#set($my_map = $rx.string.stringToMap("a=b"))


Looks cool. Also, I can confirm that 6.7 uses velocity 1.5, and the map object creation syntax of {} actually works.