SOLVED

Re: Replace characters from a string with velocity

Go to solution
OttonHJ
Level 1

Replace characters from a string with velocity

Hello Champions,

 

This is my use case: I have a lead record with HTML code, let's say "%p$Lorem ipsum dolor sit amet%/p$", if I swap the "%" for a "<" and the "$" for a ">", would be the HTML code we all know. So, I'm trying to make that replacement with velocity but couldn't find a solution, I've tried several things, but nothing seems to work for me. 

 

I'm new to the velocity scripting world and been looking around to avoid making this thread but well, here I'm. I appreciate any help you can give me. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Replace characters from a string with velocity

Well, you can‘t just make up your own encoding. There’s a reason the W3C and IETF exist! Your example doesn’t account for literal curly braces, which would by definition break the encoded value.

 

You can use URL encoding (percent encoding).

 

Encode in JavaScript on the form side:

encodeURIComponent(someFieldValue)

 

Decode in Velocity:

${link.decode($lead.someFieldValue)}

 

View solution in original post

4 REPLIES 4
SanfordWhiteman
Level 10 - Community Moderator

Re: Replace characters from a string with velocity

Sorry, but is that an actual example? Who thought to encode HTML that way instead of, well, the actual HTML &-encoding standard?

 

If that is an actual example, what are the special escapes that represent a literal % and $? You can’t just replace all percent symbols with greater-than and dollar symbols with less-than, because that would mean anytime a literal percent or dollar appeared, it would break the final HTML.

 

I’d like to hear more about the justification and/or specification for this type of encoding. It’s not standard in any way.

OttonHJ
Level 1

Re: Replace characters from a string with velocity

Thanks for your reply, long story short, we are trying to send some HTML code over a form submit, but we get an incorrect input error when adding the HTML code to the form field, so we thought on replacing the great and less symbols to any other symbols and use velocity to replace them again.

 

So, if I have this line of html code:

<p>My house is green</p>

 

Since we get the error mentioned above, we thought of replacing the symbols, doesn't have to be exactly % and $, could be "{", "}":

{p}My house is green{/p}

 

Once the code is sent we could replace the symbols back to the original HTML tag symbols.

SanfordWhiteman
Level 10 - Community Moderator

Re: Replace characters from a string with velocity

Well, you can‘t just make up your own encoding. There’s a reason the W3C and IETF exist! Your example doesn’t account for literal curly braces, which would by definition break the encoded value.

 

You can use URL encoding (percent encoding).

 

Encode in JavaScript on the form side:

encodeURIComponent(someFieldValue)

 

Decode in Velocity:

${link.decode($lead.someFieldValue)}

 

OttonHJ
Level 1

Re: Replace characters from a string with velocity

Thanks for your advice Sanford.