Velocity scripting - How to store multiple variables in a single token? | Community
Skip to main content
February 23, 2017
Solved

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

  • February 23, 2017
  • 2 replies
  • 7551 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

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.

2 replies

July 25, 2017

Hi Sanford,

I'm still working on this... and trying to use the dictionary solution. So I've created an Email Script in my tokens which goes like this:

(I've checked the correct information in the right hand panel)

But when I then call merchantName in the email subject, it doesn't get processed:

Are all the Email scripts processed in the program before the email asset is built? Or will the email only use the email scripts that are called?

I'm thinking that my email script is not being processed because I'm not callint it,which is why the merchant name doesn't appear in the subject. If this is the case, how do I call an email script in an email, just to initialize the different variables? Or should I use that email script to at least display the first variable in the email after initializing the variables?

Thanks,

Thomas

SanfordWhiteman
New Participant
July 25, 2017
  • You have to include a token for it to be processed.
  • Tokens are processed from top to bottom of the email.  Variables set in earlier tokens can be called from later tokens, but not vice versa.
  • You don't put VTL directly in the email content.  Your Subject line needs to be in another Velocity token, then you include that token as {{my.subject}}.
July 25, 2017

Thank you for your patience, Sanford.

I created a mkteditable section in my template, I edit the email asset and insert my token in the zone. But when I try to approve the email asset, I get this error:

Any idea what I'm doing wrong?

Am I using the correct syntax for thisMerchantData? Because the MID is not the same color as the Name. Or is it the Marketo parser that is not consistent?

Thanks,

Thomas

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
February 23, 2017

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.

February 23, 2017

Thanks for that Sanford. I agree, using dictionaries is great. So how would I then call this variable from the inside of my email to display the English thisButtonCityLink ?

SanfordWhiteman
New Participant
February 23, 2017

${thisButtonCityLink['en']}

To parameterize with the lead's preferred language (assuming you have such a field), like so:

${thisButtonCityLink[$lead.PreferredLanguage]}