Velocity - How to avoid ParseErrorException when using jQuery?

后端 未结 5 380
闹比i
闹比i 2021-02-04 10:23

I\'m trying to add a jQuery post to some JavaScript on a web page. The entire page is built up of several Velocity templates. Everything has been fine until I\'ve tried to add t

相关标签:
5条回答
  • 2021-02-04 11:00

    You can wrap your javascript with #[[ ... ]]# which tells Velocity to not parse the enclosed block (new in Velocity 1.7)

    #[[ 
    <script>
        ...
    </script>
    ]]#
    
    0 讨论(0)
  • 2021-02-04 11:09

    you must create a js file with your javascript code and import your js file into your vm code

    0 讨论(0)
  • 2021-02-04 11:09

    I couldn't get it to work with any of the other fixes like escaping "$" in velocity unfortunately. I got it working by loading an external js-file with the jQuery instead of writing jQuery directly in velocity. Worked out for me at least, hope it helps someone :)

    /björn

    0 讨论(0)
  • 2021-02-04 11:12

    I think this is a bug in version 1.6.x, because it works fine in 1.7(If it did not, please tell me, I test it many times..), according to the reference, the $ takes effect only when it is followed by a-zA-Z. I want to try do debug what happened really, but the translation code is generated by Java CC tool, it is too hard to recognize the logic...

    0 讨论(0)
  • 2021-02-04 11:19

    Ok, there appears to be two solutions for this:

    First, with jQuery we can just avoid using the global alias $ and instead use the jQuery object directly:

    jQuery.post(url, myJSONObject, function(result){
    ~~~snip~~~
    

    In my case, the above works great. But I suspect in other scenarios (non-jQuery) this may not be possible. In which case, we can 'hide' our character within a valid Velocity reference like this:

    #set( $D = '$' )
    ${D}
    

    Source: http://velocity.apache.org/engine/devel/user-guide.html#escapinginvalidvtlreferences

    I'd still like to know why the backslash escape didn't work, but the above will at least get me moving again. :)

    0 讨论(0)
提交回复
热议问题