Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Concatenate two variable in Velocity Code

Suresh Venkat December 23, 2020

I am creating a User Macro in Confluence using Velocity Code. Need to concatenate two variables

using below code

##set($listofContent = $labelManager.getCurrentContentForLabelAndSpace() )

## set($labelManager = $action.getLabelManager() )

##set($label = $labelManager.getLabel() )

##set($newLabel = "")

##set($oldLabel = "")

#foreach ($labelling in $page.getLabellings () )

#set ($label = $labelling.getLabel() )

#set ($oldLabel = $label)

##here I would like to concatenate Old Label + New Label, so that I can get all labels

##below command is not working

#set ($newLabel = $oldLabel$newLabel)

## where am I going wrong?

#end

1 answer

0 votes
Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2020

Well straight concatenation would just be

#set ($newLabel = $oldLabel + $newLabel)

But if $oldLabel = "foo" and $newLabel = "bar", then the result will be foobar.  So if you want some sort of delimiter, then you need to do something like this:

#set ($newLabel = $oldLabel + ', ' + $newLabel)

to result in foo, bar

Suresh Venkat December 24, 2020

@Bill Bailey : While old label prints the latest label on a page, newlabel would simply print $newLabel as the value of label, with both the options provided. 

I tried setting the initial values as 

##set($newLabel = ' ')

##set($oldLabel = ' ' )

That did not help either. Referred  and implemented from this  link http://people.apache.org/~henning/velocity/html/ch09s05.html. Still no avail.

Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 24, 2020

You do realize that ## is the syntax for a comment, so those lines will not operate?

I would also suggest looking at the latest guide:

http://velocity.apache.org/engine/2.2/user-guide.html#string-concatenation

It suggest using

#set ($newLabel = $oldLabel$newLabel)

But I have typically used the + operation as I often need to mix in HTML code. Either should work.

 

It has been awhile since I worked with labels, but you may have to convert the object to a string one of two ways:

$label.toString()

or

$label.name

 

 

Suresh Venkat December 24, 2020

Ok. will have to reach out to dev@velocity.apache.org, as trying all options has not worked. FYI, without converting oldLabel is printing the latest label as it is. Hence..

But, Thanks for offering the advice

Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 24, 2020

Also, I don't think I have seen a method .getLabellings () (note that there should not be space between the method and () ).

The method I have always used was .getLabels()

Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 25, 2020

Yeah, that article is from 2015 so I think version 5. It may be that .getlabellings() was deprecated, or a method added by another plugin?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
6.15.10
TAGS
AUG Leaders

Atlassian Community Events