Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping the dollar sign in velocity using backslash not working

Tags:

java

velocity

I have the following segment to get rendered from a velocity template file.

xyz $$foo

The dollar signs need to be escaped from VTL parser. And in the template file I represented it as

xyz \$\$foo

I expect

xyz $$foo

but I get

xyz \$foo

What am I doing wrong??

like image 403
Abinash Koirala Avatar asked Mar 21 '13 12:03

Abinash Koirala


People also ask

How do you escape velocity?

Velocity allows for explicit escaping of References and Directives using the \ (backslash) character. If the character following the \ would start a new directive or reference, then this character is output verbatim. This can lead to some unexpected behaviour, especially with directives.

What is Velocity template?

Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.


2 Answers

One of the way is to use a fixed string variable i.e, setting a variable for the $ sign and then using it.

#set ( $d = "$")
xyz ${d}${d}foo

It gives the expected output

xyz $$foo
like image 53
Abinash Koirala Avatar answered Sep 28 '22 11:09

Abinash Koirala


You might also want to check out the EscapeTool in velocity.

Once you have included it in context (e.g. under the name "esc"), you can use ${esc.d} to get the dollar sign. The method is you mentioned is easier for this particular case though.

I thought it was worth mentioning because it provides several other convenient methods to escape strings appropriate, if your templates are generating Java/javascript/html etc.

like image 35
sonofrage Avatar answered Sep 28 '22 11:09

sonofrage