SOLVED

Re: Extra Space in Email Script

Go to solution
Chelsey_Davis
Level 3

Extra Space in Email Script

Hello,

I have an extra space when I use my email script for changing an all-caps first name to sentence case.

Here's the script:

#set($name = ${lead.FirstName})

#if( !$name.isEmpty() )

${name.substring(0,1).toUpperCase()}${name.substring(1).toLowerCase()} 

#end

And when I use it in my email, it always includes a space before the comma. (example: Hi Chelsey ,)

Does anyone have any ideas as to what I need to change?

Thanks!

Chelsey

---

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Extra Space in Email Script

Velocity Template Language (VTL) is entirely space-preserving. So the linebreak after your single line of output is seen by HTML, which in turn interprets it as whitespace (per HTML rules).

If you comment out the linebreak, then it won't be output. I also recommend not using curly braces outside of output, and you also have a built-in tool to make the code tighter, so:

#set($name = $lead.FirstName)

#if( !$name.isEmpty() )

${display.capitalize($name.toLowerCase())}#end

Also make sure there aren't any empty lines at the end of your script -- these are preserved, too.

However, in general, you should be very wary of guessing how people spell their names. You'll be turning first names like "LaTasha" and "McKenna" into "Latasha" and "Mckenna".  I really hate when sites misapply "intelligence" like this.

View solution in original post

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: Extra Space in Email Script

Velocity Template Language (VTL) is entirely space-preserving. So the linebreak after your single line of output is seen by HTML, which in turn interprets it as whitespace (per HTML rules).

If you comment out the linebreak, then it won't be output. I also recommend not using curly braces outside of output, and you also have a built-in tool to make the code tighter, so:

#set($name = $lead.FirstName)

#if( !$name.isEmpty() )

${display.capitalize($name.toLowerCase())}#end

Also make sure there aren't any empty lines at the end of your script -- these are preserved, too.

However, in general, you should be very wary of guessing how people spell their names. You'll be turning first names like "LaTasha" and "McKenna" into "Latasha" and "Mckenna".  I really hate when sites misapply "intelligence" like this.

Chelsey_Davis
Level 3

Re: Extra Space in Email Script

Thanks, Sanford Whiteman !

I'll give this a try and see what happens. And I agree with you, it's not the best option for folks who have some casing in their name. But I think it's better than "LATASHA" and "MCKENNA." Is there a better script to run for this situation?

Cheers,

Chelsey

---

SanfordWhiteman
Level 10 - Community Moderator

Re: Extra Space in Email Script

But I think it's better than "LATASHA" and "MCKENNA."

I'd say it depends.

All-caps is at least a standard form used in banking and government, and it's not wrong per se; on the other hand, changing mixed case arbitrarily is more distinctly misspelling their name.

There's no algorithm that can correct the spelling of just any proper noun. If a value already is in mixed case, you could keep it as-is:

#set($name = $lead.FirstName)

#if( !$name.isEmpty() )

#set( $charMeta = {} )

#set( $nameChars = $name.toCharArray() )

#foreach( $char in $nameChars )

#if( !$charMeta.hasUpperCase && $char.isUpperCase($char))

#set( $void = $charMeta.put("hasUpperCase",true) )

#elseif( !$charMeta.hasLowerCase && $char.isLowerCase($char))

#set( $void = $charMeta.put("hasLowerCase",true) )

#end

#end

#if( !$charMeta.hasLowerCase || !$charMeta.hasUpperCase )

${display.capitalize($name.toLowerCase())}##

#else

${name}##

#end

#end

(VTL sure is verbose -- this is a seemingly minor check but thanks to the combo of Java and Velocity it's almost 20 lines.)

Note this won't work for Unicode surrogate characters. The whole idea of casing in non-Latin languages is a matter for separate study.