Search code examples
replacevelocitytrimremoving-whitespace

Removing whitespace from the output of a Velocity macro


I am calling a Velocity macro called #renderwithfallback, to which I don't have access to the source code. However, the resulting output of this macro call contains leading whitespace, and I'm having trouble removing this.

The call to the macro looks like this (note that this is preceded by a variable called $link.requestBaseURL):

$link.requestBaseURL#renderwithfallback({'content': $image, 'params':{'renderMode': 'urlBuilderStandard'}})#end

The resulting output looks like this:

http://local.london24.com:8080 /image/policy:1.312:1450097969/Yellow-and-orange-daffodils.jpg?f=default&h=200&w=300&$p$f$h$w=c7ebfa3

As you can see, the URL is broken due to the presence of the whitespace between the 8080 and the forward slash.

I have made many attempts to remove this whitespace, but nothing has worked. I have tried adding .trim() and .replace(" ","") to the end of the macro call, but this had no effect. I have also tried assigning the macro call to a Velocity variable like this:

#set($imageLocation = "#renderwithfallback({'content': $image, 'params':{'renderMode': 'urlBuilderStandard'}})#end")

and then calling the .trim() method on $imageLocation, but that didn't work either. The #set line printed out the output of the macro straight away, and then when I tried to print out $imageLocation or even $imageLocation.trim() it just came out blank on the webpage.

Does anyone know how I can get rid of the whitespace?


Solution

  • Try $stringUtils.deleteWhitespace(your variable)