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?
Asked
Active
Viewed 2.7k times
2 Answers
43
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.

Sergiu Dumitriu
- 11,455
- 3
- 39
- 62
-
6This is unfortunately not documented in the [Velocity User Guide](http://velocity.apache.org/engine/releases/velocity-1.7/user-guide.html). – Hok Sep 17 '14 at 12:07
-
2Life saver, multumesc! – Michael-O Feb 07 '16 at 22:10
2
See the Velocity Escape Tool:
https://velocity.apache.org/tools/2.0/apidocs/org/apache/velocity/tools/generic/EscapeTool.html

Alan Hay
- 22,665
- 4
- 56
- 110