Escaping quotes in velocity template

Krishna Sarma picture Krishna Sarma · Oct 25, 2013 · Viewed 20.8k times · Source

I have a java method, that takes a few strings. This method needs to be called from a Velocity Template. However, the strings are too complex, with lots of single quotes, double quotes and commas as well. As a result merge is failing. Is there a way to escape quotes in Velocity?

Answer

Sergiu Dumitriu picture Sergiu Dumitriu · Oct 29, 2013

It depends on which version of Velocity you're using. Velocity 1.7 has clear rules for escaping quotes: just double the same type of quotes used to wrap the string:

$object.callMethod('Let''s have fun with "quotes"',
  "Let's have fun with ""quotes""")

Prior to that, there were some fuzzy rules with backslash escapes that didn't always work as expected.