SOLVED

Velocity scripting - How to store multiple variables in a single token?

Go to solution
Anonymous
Not applicable

Hi there,

For each email I'm creating I have multiple email scripts because the CTA link, hero image, etc. all depend on variables (in my case the user's city)

Is it possible to store all of this in one single token instead of having one token per variable I need?

What I'd like to do is something like:

#if(${lead.City} == "Montréal")

  #set( $thisButtonCityLinkEn = "http://www.yellowpages.ca/pl/loc/montreal-qc")

  #set( $thisButtonCityLinkFr = "http://www.pagesjaunes.ca/pl/loc/montreal-qc")

  #set( $thisHeroImage = "bla bla Montreal Hero bla bla")

  #set( $thisCleanCity = "Montreal")

#elseif(${lead.City} == "Toronto")

  #set( $thisButtonCityLinkEn = "http://www.yellowpages.ca/pl/loc/toronto-on")

  #set( $thisButtonCityLinkFr = "http://www.pagesjaunes.ca/pl/loc/toronto-on")

  #set( $thisHeroImage = "bla bla Toronto Hero bla bla")

  #set( $thisCleanCity = "Toronto")

#end

And then from the inside of the email I would like to be able to call the 4 different variables I need for each email. Is this possible or does each Email Script return only 1 value? Or perhaps an array of values?

Thanks,

Thomas

Tags (1)
1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

You can easily store the data in a single Velocity variable (I strongly recommend the use of maps/dictionary objects in VTL, wherever you can)...

#set( $localData = {

  "thisButtonCityLink" : {

     "en" : "http:​//www.yellowpages.ca/pl/loc/montreal-qc",

     "fr" : "http:​//www.pagesjaunes.ca/pl/loc/montreal-qc"

  }

} )

... and this variable will be available from all subsequent tokens present in the email.

But to get different output, you have to either:

1. Use different tokens (that each output different properties from the same global variable).

or

2. Mutate the variable each time you output the (same) token, as I demonstrated in this blog post​. This approach is not for the faint of heart as it means the order of tokens directly changes the output. It's kind of like working in some long-ago programming language where each use of a variable "pops the stack." You might find it useful, though.

View solution in original post

15 REPLIES 15