append for string variable in Apache Velocity Template Language

4.5k views Asked by At

I want to construct query string in the API Gateway mapping template. I have something like this

#foreach($entry in $entries)
  #set($count = $foreach.count)
  #set($entriesQueryString = "$!{entriesQueryString}Id=${count}&"
#end

The idea is to append new string as long as there are entries provided in the input.

Is my code valid? Any other ways to do append?

1

There are 1 answers

0
Graucho Marx On

According to this post and VTL user guide page, the way concatenation is done is just by "putting items together". From the VTL guide:

A common question that developers ask is How do I do String concatenation? Is there any analogue to the '+' operator in Java?. To do concatenation of references in VTL, you just have to 'put them together'. The context of where you want to put them together does matter, so we will illustrate with some examples. In the regular 'schmoo' of a template (when you are mixing it in with regular content) :

#set( $size = "Big" )
#set( $name = "Ben" )
The clock is $size$name.

So I guess that's the only way.