Handling Empty Token Output in Marketo
When using Marketo’s lead tokens (e.g., {{lead.SomeField}}
), you might notice that an empty field doesn’t generate any spaces or placeholders (unless, of course, you mention a default value in the token)—it simply outputs nothing. However, unintended elements such as line breaks or wrapper <div>
tags can still appear in your HTML output, which may cause formatting issues.
Identifying Unintended Output
If you observe unexpected spacing or elements, consider whether the empty token is still affecting the HTML structure. For example, when entering tokens in a Rich Text box:
{{lead.SomeField1}}
{{lead.SomeField2}}
Marketo may render these with implicit <div>
tags:
<div></div>
<div></div>
These <div>
elements persist even when the token values are empty, potentially affecting layout and styling.
Ensuring a Clean Output
If you want to eliminate these extra <div>
elements when tokens are empty, you need to use Velocity scripting. The simplest way to do this is:
#if( !$lead.SomeField.isEmpty() )
<div>${lead.SomeField}</div>##
#end
This ensures that:
The
<div>
only appears when the token has a value.The
##
at the end removes trailing line breaks, keeping the output compact.
Advanced Considerations
Controlling Whitespace in Email Templates
Even hidden empty elements can cause spacing issues when working with email templates. If your design requires strict control over spacing, consider wrapping Velocity logic around entire blocks of content instead of just individual fields:
#if( !$lead.SomeField.isEmpty() )
<table>
<tr>
<td>${lead.SomeField}</td>
</tr>
</table>##
#end
This prevents unnecessary table structures from appearing in emails when the field is empty.
Handling Multiple Fields Efficiently
For scenarios where multiple tokens must be displayed together, ensure empty values don’t create unintended blank lines or visual gaps:
#set( $output = "" )
#if( !$lead.FirstName.isEmpty() )
#set( $output = "$output $lead.FirstName" )
#end
#if( !$lead.LastName.isEmpty() )
#set( $output = "$output $lead.LastName" )
#end
#if( !$output.isEmpty() )
<p>$output</p>##
#end
This approach ensures that a <p>
tag only appears when at least one field has a value.
TL;DR
By understanding how Marketo renders empty tokens and using Velocity when necessary, you can prevent unintended HTML output and maintain cleaner, more predictable formatting in your emails and landing pages.