views:

128

answers:

6

I have Apache Velocity. I have some jQuery code. I think VM doesn't like when I do things like $img.css("float","left"). How can I completely disable VM parsing within a block of HTML/Javascript ?

Thanks

A: 

You can escape the dollar sign in Velocity by preceding each $ sign with a backslash...

\$img.css("float", "left");
great_llama
And if that fails you (because of an "invalid reference")... you can create a Velocity variable to hold the dollar sign as a string value, the next section in the docs shows how and why...
great_llama
A: 

Looking at the user guide it looks like as long as you don't have a variable named $img in velocity, you shouldn't have a problem with velocity parsing it. Otherwise you can escape with \$img.

As far as actually having the parser skip over the the string as you would with a CDATA tag in XML, I'm not sure how you could do that.

aubreyrhodes
+1  A: 

For short examples, like the above, if it isn't a legitimate Velocity reference, just do $img and Velocity will ignore it.

It's tempting to escape the reference, but this is extremely quirky. If $img is a real reference, then \$img will display $img. But if $img is not a Velocity reference, then \$img will display \$img.

The best bet, especially if you have a long block of text you do not want parsed, is to put it in a separate file and use #include, which does not parse the include text.

#include("file.vm")

This will include "file.vm" directly into the output without parsing it. (If you want to include text and parse that text, use #parse).

Will Glass
A: 

The \ escaping is unreliable. Do:

context.put("D", "$");

and then

${D}img

In the upcoming 1.7, there is a new #[[ parser will ignore this completely ]]# syntax. Hopefully a 1.7-beta1 will be out soon.

Nathan Bubna
A: 

You can assign a variable to parse the dollar sign. For example:

#set( $jQ = "$" )

Now you can use this variable to place a dollar sign where you need to:

<script type="text/javascript">
$jQ img.css();
</script>

Please ensure there's a space between the $jQ variable and the img.css(); (so that velocity doesn't try to interpret the rest as a different variable). You won't have to do this if a parenthesis follows directly after the $jQ var.

This would be fine:

$jQ('#smithySword');
RedGlobe
A: 

Velocity 1.7-beta1 is now out, and it ships the #[[don't parse me!]]# directive, so you don't have to escape a bunch of code in your .vm files.

Works for me like a charm.

Brian Clozel