Using Velocity Script to Display an Image | Adobe Higher Education
Skip to main content
michellechopin
Community Advisor
Community Advisor
July 14, 2026
Question

Using Velocity Script to Display an Image

Hello Community 👋🏻

 

I use Velocity Scripts in our conference emails to pull in data points from an integrated system so that I can get creative with the content displayed. I usually only work with text content scripts and today I’m trying to create one for an image but I cannot get it to work.

 

Intent: I want to show this image if someone has NOT filled out a quiz that’s part of our registration process. i.e. the Data point is Oktane Lab Profile (oktaneLabProfile) and IS EMPTY.

 

Script: This is what Gemini gave me after I asked for help fixing the script.

## Determines whether Quiz image is shown or not
#if( !$lead.oktaneLabProfile || $lead.oktaneLabProfile == "" )
<img src="https://pages.okta.com/rs/855-QAH-699/images/Hero-Oktane-Identity-1072x400.png?version=0" alt="Find your Oktane identity" />
#end

 

Use: I inserted the token into an image module that I’m using for this section (so that the formatting/styling is used vs just adding a simple image into a text module for example).

 

Preview: This is what I see in the Marketo Preview screen - as the script is looking for the field to be EMPTY, I expect to see the image here. 

 

Additional checks:

  • I have checked the box for this field in the VS editor
  • I used a test email that I know has that field empty and I get the same visual result
  • I have two text VS modules underneath this one that are using the same field, and they work/show as expected in the Preview (default and with my test email)

 

As usual, I feel like someone’s going to point something out that’s going to make me 🤦🏻 and I’m OK with that! Can anyone help a VS noob? Thanks in advance 🙏🏻

5 commentaires

michellechopin
Community Advisor
Community Advisor
July 14, 2026

Follow up note/comment: I wanted to see what the image looked like in a token format and when I set it up as either an Image Token or a Text Token (no velocity script), the Preview renders the filename and no image (I’ve tried including and removing the https:// but I get the same result).

 

Text Token with full URL:

 

Preview renders the URL vs the image:

 

We’ve just been testing a bunch of templates with tokens and all of our images are setup like this, so I’m additionally confused as to why the basic tokens aren’t even working for me 😂

 

michellechopin
Community Advisor
Community Advisor
July 14, 2026

Two more updates:

  1. I asked Gemini for the script from scratch (didn’t provide anything) and it gave me:

    ## Determines whether Quiz image is shown or not
    #set( $targetField = $lead.oktaneLabProfile )

    ## 2. Check if the field is empty, null, or only contains whitespace
    #if( !$targetField || $targetField.trim().isEmpty() )
      ## Display the fallback image if the field IS empty
      <img src="https://pages.okta.com/rs/855-QAH-699/images/Hero-Oktane-Identity-1072x400.png" alt="Find your Oktane identity" width="570" style="display: block; border: 0;" />
    #end

  2. While this still doesn’t work if I load the token into one of our template modules for an image (I’m wondering if the code for the module is conflicting/breaking it), it works when I add an Email Script token into a standard content module.

Final solution: I used a simple content/text module in my email and added all the script tokens (essentially recreated the individual modules I had before using the script and it all works as expected). It just hit me, when talking through this with a colleague, that a Velocity Script token might simply not work in an image module? Still interested to hear what I might have done wrong, or if it is in fact, simply the type of token I’m trying to use outside of a content block 😀

SanfordWhiteman
Level 10
July 14, 2026

Depends on what you mean by “image module” but an entire <img> tag can’t be inside a mktoImg, no.

 

The code is still not correct; there’s no situation where a String field on the Person can be null or false, so checking for those just hurts readability. You don’t really need to trim() it either (why would it ever be just whitespace?). All you need is the isEmpty().

michellechopin
Community Advisor
Community Advisor
July 15, 2026

@SanfordWhiteman We have a variety of modules in our template that allow different uses for, and treatment of, images (vs putting them into a content block with text for example). I couldn’t get any of these scripts to work in there so perhaps that module doesn’t like using a VS token within it for some reason 🤷🏻

 

For the script, that’s what Gemini gave me and it worked in the email - I can remove trim() though if it doesn’t make sense/have any use/is redundant/will break things.

 

For the field I have to reference for the token, it’s one where they user/recipient gets a profile type when they fill out/complete the quiz - because there are multiple profiles, I was guided to use “Is Not Empty” because we want this information to show to people who have NOT filled out the quiz. I realize the challenge with using a null field vs a field with something specific in it (I’m not sure if those profiles are set buckets or if a recipient can generate a new one based on their information in the quiz)  😬

Level 2
July 17, 2026

Gemini, like all current AI models, doesn’t actually understand Velocity in the Marketo context. (That’s also why some people/bots have been posting garbage suggestions here recently.)

 

There’s nothing inherently wrong with using a String field.  The point I was making above was that in Velocity, !$whatever matches if the variable $whatever has either the Boolean value false or the reserved value null. Neither of these will ever be the case with a String field on the Person in Marketo — there is absolutely no way for that to match. Therefore it’s just wasting resources and making the code confusing.

 

A String field can technically have only whitespace characters, which means it won’t match isEmpty(). Some people might think to trim() all the time. But it’s overkill as it’s vanishingly unlikely any app would end up putting only spaces in a field. Not impossible, but if you’re always adding trim() it’s doing more harm than good IMO. Besides, if you’re worried about whitespace-only strings, you should trim all the fields in one place, like go over every string field on $lead and trim it:

#foreach( $entry in $lead.entrySet() )
#set( $key = $entry.getKey() )
#set( $val = $entry.getValue() )
#if( $val.class.name.equals("java.lang.String") )
#set( $lead[$key] = $val.trim() )
#end
#end

 

Level 2
July 17, 2026

P.S. Another reason to not trim() is it gives the false assurance you’re trimming “any whitespace” but if you look at the definition of whitespace in Java trim() it’s more limited than you probably think. For example, a NBSP (U+00A0) is not considered whitespace, and if your data is messy that’s exactly the kind of invisible character that creeps in!

michellechopin
Community Advisor
Community Advisor
July 17, 2026

I always appreciate the detail and knowledge you provide!

 

However, that’s so far above my head that I’m not confident at all in how to adjust my code; should I just remove the trim() part? Should I adjust the whole thing? If the latter, what would that script look like (based on the script I shared in my original message or code that Gemini spat out for me).

 

With the detail of how that field works, I was told to use “is not empty” so if that doesn’t work, what should I be using so that the script can still pull the field information correctly baed on how it’s set up?

 

As mentioned above, the script appears to be working when I test it in Marketo but I’m always up to streamline and make a script cleaner if there’s an option to do so. TIA 😀