Re: Extra whitespaces surrounding tokens

Anonymous
Not applicable

Hello.

I'm trying to use tokens in my emails, but I encountered with following problem: when I write sth like "{{my.token}}." there is a space between my token and a dot in a resulting email. As I understand it's because of line breaks that surround subsituted token in html code of email.

Is there any way to get rid of that line breaks and that space character?

Thanks!

20 REPLIES 20
Milena_Mitova
Level 2

Hi there, I found out that in addition to the above proposed solution (## at end of each line),  you need to check if you have extra unused lines in the script - it always creates space (see empty line #3 below) - so make sure you remove those as well:). Hope it helps:

#set($timeZoneObject = $date.getCalendar().getTimeZone())
$date.format("yyyy", $date.getDate(), $date.getLocale(), $timeZoneObject.getTimeZone("EST"))

Image 1 below shows the space. Once I deleted the 3rd line, the space after My.Year token is not showing any more. 

SpaceAfterToken.png

 ScriptOutputNoSpaces.pngScriptOutputNoSpaces_Tokens.png

SanfordWhiteman
Level 10 - Community Moderator

That's right, Velocity is strictly space-preserving, as I mentioned above:

if you have whitespace, it's preserved by default, because you might be outputting into plain text (not just plain-text email but any old text file) where CRs are critical for proper formatting.

So there isn't actually such a thing as an "unused" line -- a trailing CR is as important as any other CR.

If you want whitespace in your code for readability, it can help to use #define blocks instead of direct output.

For example:

#define( $output )



This

has

line

breaks

before

and

after



#end
${output.toString().trim()}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The, er, defining of the #define doesn't output anything. Output is managed by String-ifying the #define and then trim()-ing it first. So the final email wouldn't have the before and after linebreaks.

See also https://nation.marketo.com/community/product_and_support/blog/2018/09/22/in-velocity-tokens-trim-you... 

Rachel_Noble
Level 10 - Champion Alumni

We've had the same problem, and I think it's the source code from our templates re-formatting the HTML to add unwanted line breaks when we approved the emails. There's probably a better solution, but whenever I encounter this, I create a richtext token that has the value I want. Set a richtext toekn {{my.workaround}} = {{my.token}}. (including the period) and enter that in its place.

Not ideal, but worked for me multiple times.

Wyatt_Bales4
Level 4

I've seen this for multiple customers. Adding '##' after the desired output fixes it for me.

#elseif ($lead.attribute == "XYZ")

Display this##

Before it would show "... Display this ."  now it shows "... Display this."

Anonymous
Not applicable

Wyatt Bales​, that was a great (and easy) fix!

Anonymous
Not applicable

As I understand it's because of line breaks that surround subsituted token in html code of email.

Are these line breaks your additions to the HTML, or are you saying they are contained in the field (for example, a field that originated from a textarea)?

If they're in the HTML, remove 'em!

If they're in the field, you can pass the field through a Velocity (email scripting) template to massage the data, if necessary.

Anonymous
Not applicable

Nope, I suggest that these linebreaks are added by Marketo. The possible solution is to add the dot to the token but it reduces it's flexibility (for example, if I want to use that token in the middle of a sentence and at the end of it, I'll need to create two tokens instead of one)

SanfordWhiteman
Level 10 - Community Moderator

Text {{my.tokens}} don't just add linebreaks.  Please go to Email Actions >> HTML Tools >> Download HTML and post a snippet of several lines before and after the token is inserted.

Anonymous
Not applicable

We're having this same issue with a token we use for Business Name. In every instance, the token is showing the business name fine with no extra spaces before it or after it, but when we try and use the biz name right before punctuation it adds a space (not a line-break, but a space). We have confirmed this space does not exist in the html.

How it's Coded:

<div style="font-size: 16px;">Would you like to track when and how OURCOMPANY drives potential customers to {{my.account_name}}? <a href="URLHERE">Visit the Activity Page</a> to view more information about the customers connecting with your business:</div>

How It Appears: 

Would you like to track when and how OURCOMPANY drives potential customers to FAKE NAME ?

As you can see, it's adding a space when used before punctuation. It doesn't do this anywhere else it's used.

SanfordWhiteman
Level 10 - Community Moderator

Is this a Text token or is it an Email Script (Velocity) token?

Rajesh_Khatri
Level 2

Hi Sanford, 

 

I have created velocity token script for captilising the first letter of field First Name. But the problem is it is giving a whitespace after a token. 

 

For example: If I am using the token in email, it gives me as following 
Hi Sanford ,

 

I want to remove the space between name and comma. I tried your method .tostring().trim() but it did not work. Here below is my script. Can you help me with code that can remove the white space.  

 

#set($name = ${lead.FirstName})
#if(!$name.isEmpty())
${name.substring(0,1).toUpperCase()}${name.substring(1).toLowerCase().toString().trim()}
#end
 

Regards

Rajesh Khatri

Anonymous
Not applicable

Email Script/Velocity token.

SanfordWhiteman
Level 10 - Community Moderator

Well, then you probably have an extra CR in the output.  Remember, Velocity is not purely for HTML output -- that is, if you have whitespace, it's preserved by default, because you might be outputting into plain text (not just plain-text email but any old text file) where CRs are critical for proper formatting.

Anonymous
Not applicable

If there were an extra space in the output wouldn't that result in the biz name having a space after it in EVERY instance it's used? As mentioned, it's only happening when we use this token before punctuation. I'm not entirely sure what at CR is, but I''ll forward your note to the engineer who made this token. Not seeing any obvious spaces when I look at the script:

#set($AcctName = ${Account_Contact__cList.get(0).Account_Name__c})

#if ($AcctName)

$AcctName

#else your business#end

SanfordWhiteman
Level 10 - Community Moderator

Unless you can provide a full dump of the HTML with and without the token, and the full text of the Velocity script (hard to paste this accurately into the Community, unfortunately, since the editor changes whitespace) it's impossible to verify what's happening. 

But the idea that the token output changes based on the characters after it doesn't make any sense. The Velocity parser doesn't "peek ahead" at what's next before deciding what to output.

And again, I can't repro this behavior. A ? after a token doesn't add any additional characters.

2016-10-10 15_24_31-Program Lab 09.Lab - VTL tests 02.png

2016-10-10 15_22_10-Marketo Email Previewer _ Program Lab 09.Lab - VTL tests 02.png

Anonymous
Not applicable

Yeah I don't think I'd be allowed to do that, BUT we have the exact same token in another email that DOESN'T have the issue before punctuation. There is literally no difference in the token at all, yet it works fine. We copy and pasted the script from the one token to the other and the issue is solved. Still no real explanation, but we changed nothing in the html so it definitely does seem to be related to the token.

SanfordWhiteman
Level 10 - Community Moderator

Copy and pasting removes some special (high) characters from text. So you probably had a trailing special character in there that you couldn't see (for example, a zero-width space).

Jim_Thao7
Level 9

Can you provide screenshots or examples of what the value of your token is and how it resolves?

Are you saying that {{my.token}} where the token value = "something" resolves as "something ."?

Anonymous
Not applicable

Nope, {{my.token}}. renders as "something ." (note the space before dot). In the example below, the price is my token and there is an extra space between dot and token, which is missing in the email editor. Screenshot from 2016-08-16 11-34-04.png

Screenshot from 2016-08-16 11-35-37.png

In the HTML code of rendered email you can see a linebreak right after my token which results in a space between token and dot

Screenshot from 2016-08-16 11-38-50.png

Jim_Thao7
Level 9

I feel like maybe there is a hidden character here.  Was the value "(US $89.00 per year)" copied over from somewhere? Perhaps you also copied over a hidden character of some sort. 

I would recommend that you retype this value out manually into the token to see if that helps.  Similarly, try copying and pasting this into notepad 2 or notepad++ to see if the hidden character can be revealed.