SOLVED

Script token adding an extra space

Go to solution
Anonymous
Not applicable

Script token adding an extra space

Tokens thats are velocity scripts are giving me a hard time. Some are adding extra spaces after the token is added to the content of the email. The extra spaces only show up after the email is sent and are not reflected in any previews. Does anyone know how to ensure if I add a script token, the spacing before or after the token appear normal in the sent email?

#set($frid = ${DonationList.get(0).fundraiserID})

#set($raised = ${DonationList.get(0).donationSumNew})

#set($currency = ${DonationList.get(0).currencyChoice})

#foreach($fr in $DonationList)

#if($fr.fundraiserID > $frid)

        #set($raised = $fr.donationSumNew)

            #set($currency = $fr.currencyChoice)

            #set($frid = $fr.fundraiserID)

          #end

      #end

  #if ($currency == 'EUR')

  #set($finalLTR = '€'+ $number.format($raised))

      $finalLTR

#elseif ($currency == 'GBP')

#set($finalLTR = '£' + $number.format($raised))

    $finalLTR

#else

#set($finalLTR = '$' + $number.format($raised))

    $finalLTR

#end

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Script token adding an extra space

It's not that Velocity is adding extra spaces, it's that Velocity is totally space-preserving. Any spaces in-between directives in your script are literal spaces: they will not be compacted or stripped out. This is because Velocity is at root a text output language, not an HTML output language or any other particular text-based format. The parser has no innate knowledge of which output formats might be likely to want spaces stripped out, and it wouldn't make sense to strip spaces without knowing where the output is going to go.

That is all to say: you have the whitespace in your Velocity script. That includes the spaces before the #set directives and even includes the final line break.  If you want to eliminate that, end with

#if ($currency == 'EUR')

#set($finalLTR = '€'+ $number.format($raised))

#elseif ($currency == 'GBP')

#set($finalLTR = '£' + $number.format($raised))

#else

#set($finalLTR = '$' + $number.format($raised))

#end

$finalLTR##

P.S. Your currency display options won't work. You can't use generic $number.format for EUR because it uses commas as separators.

View solution in original post

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Script token adding an extra space

It's not that Velocity is adding extra spaces, it's that Velocity is totally space-preserving. Any spaces in-between directives in your script are literal spaces: they will not be compacted or stripped out. This is because Velocity is at root a text output language, not an HTML output language or any other particular text-based format. The parser has no innate knowledge of which output formats might be likely to want spaces stripped out, and it wouldn't make sense to strip spaces without knowing where the output is going to go.

That is all to say: you have the whitespace in your Velocity script. That includes the spaces before the #set directives and even includes the final line break.  If you want to eliminate that, end with

#if ($currency == 'EUR')

#set($finalLTR = '€'+ $number.format($raised))

#elseif ($currency == 'GBP')

#set($finalLTR = '£' + $number.format($raised))

#else

#set($finalLTR = '$' + $number.format($raised))

#end

$finalLTR##

P.S. Your currency display options won't work. You can't use generic $number.format for EUR because it uses commas as separators.

Anonymous
Not applicable

Re: Script token adding an extra space

Hi Sanford,

Thanks for all your help. I think I may have messed up the script again as this time it does not show the period at all after the script token in the email.

#set($frid = ${DonationList.get(0).fundraiserID})

#set($raised = ${DonationList.get(0).donationSumNew})

#set($currency = ${DonationList.get(0).currencyChoice})

#foreach($fr in $DonationList)

#if($fr.fundraiserID > $frid)

#set($raised = $fr.donationSumNew)

#set($currency = $fr.currencyChoice)

#set($frid = $fr.fundraiserID)

#end

#end

#if ($currency == 'EUR')

#set($finalLTR = '€'+ $number.format($raised))

#elseif ($currency == 'GBP')

#set($finalLTR = '£' + $number.format($raised))

#else

#set($finalLTR = '$' + $number.format($raised))

#end

$finalLTR##

Also what should be used instead of the $number.format for EUR?

SanfordWhiteman
Level 10 - Community Moderator

Re: Script token adding an extra space

When posting code, please use the syntax highlighter function in the Advanced Editor. Otherwise it's impossible to read.

pastedImage_0.png

Thanks for all your help. I think I may have messed up the script again as this time it does not show the period at all after the script token in the email.

The token won't touch things outside the token.

Also what should be used instead of the $number.format for EUR?

You have to be locale-aware.

Within EUR alone, there's not just one display format.  For example, Italy and Germany use periods as the thousands separator, while France uses a space for that. (Both use a comma for the decimal separator, which is why you can't use the generic format.) If you don't have better info, using Italy/Germany is more portable.

This is an example of how to build accurate localized emails:

#set( $locales = $field.in($number.getLocale().getClass()) )

#set( $currencies = {

  "EUR" : {

    "symbol" : "€",

    "locale" : $locales.GERMANY

  },

  "GBP" : {

    "symbol" : "£",

    "locale" : $locales.UK

  },

  "USD" : {

    "symbol" : "$",

    "locale" : $locales.US

  },

  "*" : {

    "symbol" : "$",

    "locale" : $locales.US

  }

})

#set($frid = $DonationList[0].fundraiserID)

#set($raised = $DonationList[0].donationSumNew)

#set($currency = $DonationList[0].currencyChoice)

#foreach($fr in $DonationList)

#if($fr.fundraiserID > $frid)

#set($raised = $fr.donationSumNew)

#set($currency = $fr.currencyChoice)

#set($frid = $fr.fundraiserID)

#end

#end

#if ( !$currencies.containsKey($currency) )

#set( $currency = "*" )

#end

$currency

$currencies[$currency]

#set( $finalLTR = $currencies[$currency].symbol + $number.format("", $raised, $currencies[$currency].locale) )

$finalLTR##

Anonymous
Not applicable

Re: Script token adding an extra space

#set($frid = ${DonationList.get(0).fundraiserID})

#set($raised = ${DonationList.get(0).donationSumNew})

#set($currency = ${DonationList.get(0).currencyChoice})

#foreach($fr in $DonationList)

#if($fr.fundraiserID > $frid)

#set($raised = $fr.donationSumNew)

#set($currency = $fr.currencyChoice)

#set($frid = $fr.fundraiserID)

#end

#end

#if ($currency == 'EUR')

#set($finalLTR = '€'+ $number.format($raised))

#elseif ($currency == 'GBP')

#set($finalLTR = '£' + $number.format($raised))

#else

#set($finalLTR = '$' + $number.format($raised))

#end

$finalLTR##

Apologies. Weird the period that's outside the token is still disappearing. I've attached a couple screenshots.

SanfordWhiteman
Level 10 - Community Moderator

Re: Script token adding an extra space

Why don't you just put it inside the script, since you're always outputting something anyway?

Or try replacing it with HTML .