0

I have a map in a velocity template that is dynamically modified. A simplified example to illustrate the point is below.

#set($testMap = {})

## this stuff isn't static so I can't just create 
## the map as key-value pairs in the declaration

$testMap.put("a", "A")
$testMap.put("b", "B")

$testMap

The above generates

$testMap.put("a", "A") $testMap.put("b", "B") {a=A, b=B}

I don't want the "put" instructions to show in the template.

geco17
  • 5,152
  • 3
  • 21
  • 38

2 Answers2

0

I suggest you initialize your map with entries:

#set (testMap = {"a" :  "A" , "b" : "B"})
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Hi, thanks for your input. I would have liked to do that but the map's contents aren't known when it's initialized; the `put` instructions are done later dynamically inside of a loop and a few conditions. – geco17 Jan 03 '19 at 10:19
0

To stop it from printing the instruction, I used dummy assignment variables

#set($testMap = {})

## this stuff isn't static so I can't just create 
## the map as key-value pairs in the declaration

#set($tmp = $testMap.put("a", "A"))
#set($tmp = $testMap.put("b", "B"))

$testMap

This prints

{a=A, b=B}

A similar question is here.

I don't think it's a duplicate; Map.put(K, V) returns the V which isn't the same as the string literal $testMap.put("a", "A").

geco17
  • 5,152
  • 3
  • 21
  • 38