Velocity scripting query

Velocity scripting query

This question is not answered

Hi,

I need to build a string from three variables separated with “-“.  I tried to use $core_v2_utility.Join() following is the code snippet for the same:

#set($firstName = "John")

#set($surname = "Smith")

#set($age = "25")

#set($strarr = [$firstName, $surname, $ age])

#set($stringResponse = $core_v2_utility.Join("-", $core_v2_utility.MakeList($strarr)))

$stringResponse ## Displays result

Here,

I am expecting output string as   "John-Smith-25", but I am getting empty value.

Can you please let me know what modifications should be needed in the code above to build the expected string.

Thanks.

All Replies
  • String concatenation is pretty straight forward.  Really all you need to do is “$string1$string2$string3”.  If you need a string constant : “$string1-$string2-$string3”
     
    Hopefully that will work for you
     
    Patrick Mason
    Development Lead- Extensibility
    o: 214.420.1329
    telligent.com
     
    From: anandabakhal [mailto:bounce-anandabakhal@communities.telligent.com]
    Sent: Wednesday, May 02, 2012 6:39 AM
    To: bugsandissues@communities.telligent.com
    Subject: [Bugs and Issues - Telligent Community] Velocity scripting query
     

    Hi,

    I need to build a string from three variables separated with “-“.  I tried to use $core_v2_utility.Join() following is the code snippet for the same:

    #set($firstName = "John")

    #set($surname = "Smith")

    #set($age = "25")

    #set($strarr = [$firstName, $surname, $ age])

    #set($stringResponse = $core_v2_utility.Join("-", $core_v2_utility.MakeList($strarr)))

    $stringResponse ## Displays result

    Here,

    I am expecting output string as   "John- Smith -25", but I am getting empty value.

    Can you please let me know what modifications should be needed in the code above to build the expected string.

    Thanks.

  • Hello Pratrick,

    I tried with the way suggested, as below:

    #set($firstName = "John")

    #set($surname = "Smith")

    #set($age = "25")

    #set($strarr = "$firstName-$surname-$age")

    But, I am getting result as “$firstName-$surname-25” in $strarr variable.

    It seems that it is considering the variable name as “$firstName-“ not “$firstName” since “-“ is attached to the variable name

    Can you please suggest, How can I add the “-“ without space while concatenation?

    Thanks.

  • Try changing “ doubles to single quotes and see if that helps, sometimes I reverse them

    #set($strarr = ‘$firstName-$surname-$age’)

    If not let me know and I will play around with it
    Patrick Mason
    Development Lead- Extensibility
    o: 214.420.1329
    telligent.com
     
    From: anandabakhal [mailto:bounce-anandabakhal@communities.telligent.com]
    Sent: Wednesday, May 02, 2012 7:54 AM
    To: bugsandissues@communities.telligent.com
    Subject: RE: [Bugs and Issues - Telligent Community] Velocity scripting query
     

    Hello Pratrick,

    I tried with the way suggested, as below:

    #set($firstName = "John")

    #set($surname = "Smith")

    #set($age = "25")

    #set($strarr = "$firstName-$surname-$age")

    But, I am getting result as “$firstName-$surname-25” in $strarr variable.

    It seems that it is considering the variable name as “$firstName-“ not “$firstName” since “-“ is attached to the variable name

    Can you please suggest, How can I add the “-“ without space while concatenation?

    Thanks.

  • It's the hash marks in between causing the problem.  Whenever you have a velocity string variable preceding a regular string, you have to encase the velocity variable string in brackets {}.  Try this:

    #set($firstname = "John")

    #set($lastname = "Doe")

    #set($age = "25")

    <p>${firstname}-${lastname}-$age</p>

  • Good catch J  Completely missed that one
     
    Patrick Mason
    Development Lead- Extensibility
    o: 214.420.1329
    telligent.com
     
    From: sthaynes [mailto:bounce-sthaynes@communities.telligent.com]
    Sent: Wednesday, May 02, 2012 8:15 AM
    To: bugsandissues@communities.telligent.com
    Subject: RE: [Bugs and Issues - Telligent Community] Velocity scripting query
     

    It's the hash marks in between causing the problem.  Whenever you have a velocity string variable preceding a regular string, you have to encase the velocity variable string in brackets {}.  Try this:

    #set($firstname = "John")

    #set($lastname = "Doe")

    #set($age = "25")

    <p>${firstname}-${lastname}-$age</p>

  • if you want to assign it to another velocity variable, you can do this:

    #set($fullname = "${firstname}-${lastname}-$age")

  • Perfect !!!

    It worked for me. Thanks.

    Do you have a velocity reference documentation. Because API documentation does not have much detailing about this.