Hello,
We are working on a Year in Review email that will pull in customer data through a marekto only field. We are using the code below to pull in all of our data and formatting it to include a comma on the larger numbers:
#set($YIR_most_thank_yousCapped = ${lead.mostThankYousYIR})
#set( $YIR_most_thank_yousFormatted = $number.format( "${esc.h},${esc.h}${esc.h}${esc.h}", $YIR_most_thank_yousCapped ) )
$YIR_most_thank_yousFormatted
As I am discussing this code with our in house developer, he suggested we use "#,###" instead of "${esc.h},${esc.h}${esc.h}${esc.h}" as a best practice using this code below:
#set($YIR_most_thank_yousCapped = ${lead.mostThankYousYIR})
#if($YIR_most_thank_yousCapped && $YIR_most_thank_yousCapped != "")
#set($YIR_most_thank_yousFormatted = $number.format("#,###", $YIR_most_thank_yousCapped))
$YIR_most_thank_yousFormatted
#else
0
#end
When we use this code, however, the email preview breaks. I am wondering why the second code won't register in the email?
Solved! Go to Solution.
Single quotes and double quotes have different meaning in VTL, unlike in other languages. (Single quotes do not dereference variables, they’re taken literally.)
Rather than switching between single and double quotes based on content, standardizing on double quotes and escaping reserved characters inside double quotes is the more portable approach, because you typically do want to dereference variables inside a string, with the sole exception of literal tokens like #
and $
.
Can you please highlight your code using the syntax highlighter ("Insert/edit Code Sample") so it's readable? Then we'll continue.
Your developer is mistaken and it sounds like they lack Velocity experience. The correct practice is to escape the #
character as it has special meaning in VTL.
Certainly not "best practice" to use reserved tokens without escaping – neither in VTL nor any other language!
#set($YIR_most_thank_yousCapped = ${lead.mostThankYousYIR}) #set( $YIR_most_thank_yousFormatted = $number.format( "${esc.h},${esc.h}${esc.h}${esc.h}", $YIR_most_thank_yousCapped ) )
$YIR_most_thank_yousFormatted
Above is the code I originially used.
#set($YIR_most_thank_yousCapped = ${lead.mostThankYousYIR})
#if($YIR_most_thank_yousCapped && $YIR_most_thank_yousCapped != "")
#set($YIR_most_thank_yousFormatted = $number.format("#,###", $YIR_most_thank_yousCapped))
$YIR_most_thank_yousFormatted
#else
0
The code our developer originally suggested is above.
We have also tested this code and it seems to be working
#set($YIR_most_thank_yousCapped = $lead.mostThankYousYIR)
#set($YIR_most_thank_yousFormatted = $number.format( '#,###', $YIR_most_thank_yousCapped ) )
$YIR_most_thank_yousFormatted
Single quotes and double quotes have different meaning in VTL, unlike in other languages. (Single quotes do not dereference variables, they’re taken literally.)
Rather than switching between single and double quotes based on content, standardizing on double quotes and escaping reserved characters inside double quotes is the more portable approach, because you typically do want to dereference variables inside a string, with the sole exception of literal tokens like #
and $
.
P.S. A lead field can never be null
in Velocity, so you only need
if( !$YIR_most_thank_yousCapped.isEmpty() )