0

was hoping you could help me figure out the best way to escape an apostrophe (single-quote) in my VTL syntax. Here is what is working for all of my values that do not have an apostrophe. I have tried to add an additional apostrophe, and tried a backslash in front of the apostrophe - both did not work. Thanks in advance for your help!

#set($a=${ITEM_STAGING.Item_Gift_Wrap_Price})##
#if (${ITEM_STAGING.Department== "MEN'S DEPARTMENT"})##
#set($a="10.50")
$a##
#else##
$a##
#end##

The issue is the apostrophe after the word "MEN".

Thanks in advance for your help - BC

1 Answers1

0

It's not an escaping problem, it's a syntax problem. You aren't supposed to enclose the condition itself in curly braces (and you don't need any curly braces here, see the doc). You should write:

#set($a=${ITEM_STAGING.Item_Gift_Wrap_Price})##
#if ($ITEM_STAGING.Department == "MEN'S DEPARTMENT")##
#set($a="10.50")
$a##
#else##
$a##
#end##

Escaping is to be considered only if you want a single quote inside a single-quoted string, or a double quote inside a double-quoted string. And in those cases, the best way is to double the quote, as in: 'MEN''S DEPARTMENT'.

Claude Brisson
  • 4,085
  • 1
  • 22
  • 30
  • I appreciate the input, but this rule only works with those curly braces present. It also will work if I remove that pesky apostrophe, which is last resort. I tried to escape with adding an additional apostrophe, but that did not work. Thanks for trying, though. Really appreciate your time! – BenjaminC4 Oct 31 '20 at 17:41
  • @BenjaminC4 What do you mean by "this rule only works with those curly braces present"? Can you remove them and show us your code? It is not a valid Velocity syntax to put the `==` inside the curly braces. – Claude Brisson Oct 31 '20 at 22:18
  • Hi and thanks again. When I removed them, I used the same syntax you had provided and my application did not process the rule. When I add in the curly braces and use the original code, but using a department that does not have an apostrophe in it, the application processes the rule just fine. For instance: #set($a=${ITEM_STAGING.Item_Gift_Wrap_Price})## #if (${ITEM_STAGING.Department== "HOME GOODS"})## #set($a="10.50") $a## #else## $a## #end## – BenjaminC4 Nov 02 '20 at 15:44
  • If I use the syntax you propose, I get `ERROR org.apache.velocity.parser - template.vtl: Encountered "==" at line 2, column 31`. The template is not even parsed. Without the curly braces, can you print `${ITEM_STAGING.Item_Gift_Wrap_Price}` and `${ITEM_STAGING.Item_Gift_Wrap_Price.class.name}` for debugging purposes? – Claude Brisson Nov 03 '20 at 07:48