English 中文(简体)
Velocity (VM) template: how to disable for a block of jquery
原标题:

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

最佳回答

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).

问题回答

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.

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.

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.

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

$img.css("float", "left");

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 );

The $ in jquery is a shorthand for jQuery, therefore you can substitute any $ with jQuery





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签