Skip to main content
November 3, 2015
Question

Can Velocity process program tokens?

  • November 3, 2015
  • 2 replies
  • 3601 views

If I just want to pass an array of values through an API call as a program token, can I use a Velocity token to format those values? If so, how?

Example:

{{my.ProductList}} = "Name1~ImageUrl1|Name2~ImageUrl2" (or could be json or some other format)

{{my.VelocityScript}} ==> <table><tr><td>Name1</td><td><img src='ImageUrl1'><td></tr><tr><td>Name2</td><td><img src='ImageUrl2'><td></tr></table>

I would just like to NOT have to do email presentation in my source application. I can't think of any other way, but I'm not sure if Velocity can even process program tokens.

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

2 replies

April 26, 2017

Hi Colin

I just tested this, and couldn't get it to work. I've also once read that the "Calendar File"-token is the only token that can refer to other program tokens.

SanfordWhiteman
Level 10
April 26, 2017

Aside from a certain trick I won't get into now, Velocity *can* process Program Token content -- if the other tokens are also Velocity tokens! This is why I use VTL in lieu of text tokens much of the time.

BUT! The problem is the temporary tokens you can pass via API don't support VTL. Whether an oversight or a bug, this is the main problem in your scenario.

If you can update a VTL token first using the Asset API, that works, but not all apps will work with this sort of timing.

Rachit_Puri2
Level 5
April 27, 2017

Hi Sanford, can you share some example, where you used program level script tokens within a script token? I would like to try it at my end, but want to see how it really works.

SanfordWhiteman
Level 10
April 27, 2017

Sure.

One example is including data (non-output) {{my.dataToken}} like this in the <head>:

#set( $language = "en-us" )

Then you can reference any of its variables in a subsequent {{my.outputToken}}:

Your language is ${language}!

This works because Velocity uses a persistent (i.e. reusable/reentrant) global context.  Each time you reenter the Velocity world, you see all its variables.

I use this approach almost all the time, where data tokens are kept separate from output tokens. You can't make Velocity into a pretty language, but you can make it a lot like a real language in terms of separation of concerns, etc.

The pattern I call polymorphic tokens also takes advantage of the same functionality: http://blog.teknkl.com/polymorphic-tokens/

As I said above, the dream is to be able to pass temporary VTL tokens to Request Campaign. Unfortunately, this won't work now, and the static value of the token will be used instead.