Apache Velocity is a powerful tool which allows you to transform data into an output format via VM file. It can be used to output HTML files, XML files, JSON objects, etc. A common problem I run into is using the dollar sign in a VM file when we want to parse jQuery/javascript code. There are multiple ways to alleviate this issue, but I find that replacing the dollar sign with 'jQuery' gets the job done.
In your .VM file:
//Velocity doesn't like this dollar sign.
$(document).ready(function() {
//some code here
});
Instead write one of these:
//Skip using dollar sign completely
jQuery(document).ready(function() {
//some code here
});
//Tells Velocity to parse a dollar sign
${esc.d}(document).ready(function() {
//some code here
});
//Tells Velocity not to even bother parsing
//This option is for 1.7 and higher I believe
#[[
$(document).ready(function() {
//some code here
});
]]#
//Altenative way of escaping the dollar sign
//Try not to do this one
\$(document).ready(function() {
//some code here
});
This issue is well documented, however you have to either read through all the responses to the question asked. Instead, I simply provided a list of common solutions for this problem. I've actually had problems with using the backslash with the dollar sign, so I would try to avoid that solution.
Good luck!
No comments:
Post a Comment